On Sat, Feb 14, 2015 at 6:03 AM, Xinchen Hui <larue...@php.net> wrote:

> Hey:
>
>
>
> On Sat, Feb 14, 2015 at 11:18 AM, Andrea Faulds <a...@ajf.me> wrote:
> > Hi everyone,
> >
> > I’ve written a small RFC and patch to add a “void” return type:
> >
> > https://wiki.php.net/rfc/void_return_type
> >
> > Please have a read over it. I think this would be a simple, yet useful
> addition.
> be honest, I think it's only part of the idea is implemented.
>
> which make it useless.
>
> in PHP, even if you don't return anything, NULL is returned implicitly.
>
> even if a function is declared return nothing(void).
>
> like:
>
>    function a() : void {};
>
> following codes  still works:
>
>    $b = a();
>
> so, if you want a void return type, and if you want it to be a useful
> feature..
>
> above expr should be invalid with an error " a() return nothing"
>
> so, I am -1 on this in complete RFC
>

Hi Xinchen!

While doing something like $b = a() where a() is a void function does not
make sense usually (and e.g. C will generate a warning/error for this) in
PHP there are some cases where you would want to use the return value of a
void function. Namely those are cases where you aren't calling a specific
function, but a callback that can return anything instead. E.g. consider
something like a partial application helper:

    function bind($fn, ...$bindArgs) {
        return function(...$args) use($fn) {
            return $fn(...$bindArgs, ...$args);
        };
    }

Right now this would work no matter how $fn is declared. However if you
disallow using the return value of void functions, you'd have to create an
extra bindVoid() function which does exactly the same thing, just without
the "return".

As such I don't think we can reasonably forbid using the return value of a
void function. The utility of the "void" return type in PHP would be
self-documenting function signatures. An IDE (or other static analyzer)
would of course detect cases where the return value of a void function is
used.

Nikita

Reply via email to