Andrea Faulds wrote on 14/10/2015 22:52:
Good evening,

I'm reviving my Void Return Type RFC, this time for PHP 7.1:

https://wiki.php.net/rfc/void_return_type

Please read it and tell me your thoughts!

My feeling is that it is odd to have both of these be true at once:

- void functions can appear in expressions, where the return value is interpreted as null
- "return;" and "return null;" are no longer treated as equivalent

If the intent is to say "you can use this in an expression, but its value will always be null so there's no point", then "return null;" seems just as valid as "return;", and a typehint of "null" seems to make more sense.

I take the point about making intent clear, but can't think of any other interpretation of "this function always returns null" than "this function has no meaningful result". In other words, I can't imagine ever wanting to use "null" to mean something different from "void" in this context.


I can see the point in denying the right to say "return some_function_expected_to_return_null();" But in a sense this is no different from declaring that a function returns int, and then writing "return some_function_expected_to_return_int();"

If a void function can be used in an expression, it can be used in a return expression, and if so, it feels natural for the type hint to propagate:

function foo(): void { do_something(); }
function wrapped_foo(): void { do_something_else(); return foo(); }
// ERROR: can't specify return value in a void function

"return foo()" is not illegal because foo is declared void, but because wrapped_foo is - even though the result is exactly as expected.

Regards,
--
Rowan Collins
[IMSoP]

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to