-
Notifications
You must be signed in to change notification settings - Fork 7.9k
void return type #1576
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
void return type #1576
Conversation
Question: how will the compiler deal with:
Will it throw an error, or will it assign I mean, it should be disallowed in compile-time, not in runtime. |
It just assigns We could change this, but it would break existing code, and it's inconvenient (can't safely pass along the return value of a callback, for instance). |
So, to caller POV, it makes no difference if the function is prohibited from returning a value? The only place the Seems a little odd that I can forbid a return, and use the function return (which is inexistent and cannot even be "You can't return null, but null will still be returned" stikes me as an odd design choice. I agree it's inconvenient, but that's the inconvenience strict typing brings. Most languages would complain about using a void function return as a value, wouldn't them? |
Some do, some don't. It doesn't really matter if the caller gets a value or not. |
I kind of agree, but the same applies to accessing a variable that wasn't initiated. It throws an |
I see a point where void returning functions should be allowed in callbacks, but isn't this an edge case that should be treated as an exception? I think it's better to get compile time errors when using the return value of such a function, that would prevent users to get unexpected results, especially when updating libraries that returned some result, but will return void after updating. Anyway, this is something I desperately wish to see implemented. |
What about this: We have |
@IceReaper though I partially agree it's somewhat related, it's counter intuitive and it adds additional, abnormal functionality to the concrete declaration |
@naroga However, considering all functions as returning a value which will simply be Consider this function: function foo<T>(function ():T $bar):Baz<T> { /* ... */ } If you already have a function returning function bar(function ():void $boo) {
$baz = foo<null>($boo);
// ...
} If the return value of a function bar(function ():void $boo) {
$baz = foo<null>(function ():null use ($boo) { $boo(); return null; });
// ...
} In other words you will be forced to convert between functions returning |
The RFC has now been accepted: http://news.php.net/php.internals/89110 So, this should hopefully be merged sometime soon. Is there anything I've missed with the pull request at present? |
@@ -975,6 +993,8 @@ static int zend_verify_internal_return_type(zend_function *zf, zval *ret) | |||
} else if (ret_info->type_hint == _IS_BOOL && | |||
EXPECTED(Z_TYPE_P(ret) == IS_FALSE || Z_TYPE_P(ret) == IS_TRUE)) { | |||
/* pass */ | |||
} else if (ret_info->type_hint == IS_VOID) { | |||
zend_verify_void_return_error(zf, zend_zval_type_name(ret), ""); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since internal 'void' functions (the ones we mark in prototypes and the manual with void
) actually have to explicitly return null, the internal return type checking path for them ought to permit returning null.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Come to think of it, why bother enforcing internal return types anyway?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's just for debugging. We don't do internal function return type checks in release builds.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, gotcha.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh wait, I don't need to change anything. NULL will already be accepted if allow_null
is set to 1
.
Though that makes reflection info a bit weird.
c8532dd
to
3c4f88d
Compare
3c4f88d
to
62fefcf
Compare
62fefcf
to
366ba41
Compare
Counterpart to RFC: https://wiki.php.net/rfc/void_return_type
Based on and essentially reviving this earlier pull request: #1084