Bug #79517 [Opn->Nab]: Cannot specify "void" as return type of arrow function
Edit report at https://bugs.php.net/bug.php?id=79517&edit=1
ID: 79517
Updated by: nikic@php.net
Reported by: pinkumohikan+phpbugs at gmail dot com
Summary: Cannot specify "void" as return type of arrow
function
-Status: Open
+Status: Not a bug
Type: Bug
Package: Unknown/Other Function
Operating System: macOS Mojave
PHP Version: 7.4.5
Block user comment: N
Private report: N
New Comment:
fn() => x is equivalent to function() { return x; }. Returning a value from void is illegal, thus
the error. Whether the actual returned value is null does not matter for that.
What you want will only be possible if and when arrow functions support a block syntax like fn():
void { noReturnVoid() }.
Previous Comments:
------------------------------------------------------------------------
[2020-04-24 08:40:57] pinkumohikan+phpbugs at gmail dot com
Description:
------------
When 'void' is specified as return type of arrow function, fatal error will be caused.
Even if it returns void, it will not be fatal error if the return type is not specified.
Test script:
---------------
function returnVoid(): void
{
}
var_dump(array_map(fn (int $n) => returnVoid(), [1, 2, 3]));
var_dump(array_map(fn (int $n): void => returnVoid(), [1, 2, 3]));
Expected result:
----------------
array(3) {
[0]=>
NULL
[1]=>
NULL
[2]=>
NULL
}
array(3) {
[0]=>
NULL
[1]=>
NULL
[2]=>
NULL
}
Actual result:
--------------
array(3) {
[0]=>
NULL
[1]=>
NULL
[2]=>
NULL
}
PHP Fatal error: A void function must not return a value in php shell code on line 1
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79517&edit=1
Thread (3 messages)