Hi Levi,
Levi Morrison wrote:
On Wed, Oct 14, 2015 at 4:00 PM, Stanislav Malyshev <smalys...@gmail.com> wrote:
Hi!
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!
I still see no point in this, as every PHP function actually returns
something (at least null). So this type would not actually be right and
would not reflect what actually is happening.
I agree that `void` doesn't make sense given that we document that
`return;` will return null[1].
Both you and Stas have said this, but it's only true if we solely
consider C-like languages. Other languages do different things. In the
PHP manual, Hack, TypeScript, ActionScript, and most likely other
languages (these are just off the top of my head), `void` functions do
still have an implicit result.
All of these languages would have had the choice to do what you're
suggesting and use `null`, or its equivalent (`undefined` for TypeScript
and ActionScript). They didn't. Why? If I had to guess, there's at least
three reasons. For one, void is the word languages usually use for this.
For another, `void` and `null` they mean different things. `void`
signifies a function isn't returning anything. `null` signifies a
function that *returns null*, regardless of where that null came from.
`function foo(): null { return some_probably_null_returning_function();
}` should surely be legal with a `null` type hint, yet it's nonsensical
code. Finally, making a function truly "return nothing", i.e.
disallowing its use as an expression/rvalue, breaks some use cases, like
passing along the result of a callback.
PHP would neither be the first nor the last to be using `void` in this way.
If the union types RFC[2] passes it
makes sense to allow `Foo | null` which allows something of type `Foo`
or `null`. To me it makes sense that if you then remove `Foo` you are
left with `null`, not `void`. My personal recommendation because of
this would be to use `null` for the return type and instead of `void`.
`null` would be a weird type, because it doesn't make sense as a
parameter type, and as a return type, you don't really want to enforce
returning null, you want to enforce not returning at all (see the
example above). It feels like a poor man's substitute to me.
Thanks.
--
Andrea Faulds
http://ajf.me/
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php