php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79517 Cannot specify "void" as return type of arrow function
Submitted: 2020-04-24 08:40 UTC Modified: 2020-04-24 09:14 UTC
From: pinkumohikan+phpbugs at gmail dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 7.4.5 OS: macOS Mojave
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
38 + 40 = ?
Subscribe to this entry?

 
 [2020-04-24 08:40 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-04-24 08:44 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2020-04-24 08:44 UTC] nikic@php.net
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() }.
 [2020-04-24 09:14 UTC] pinkumohikan+phpbugs at gmail dot com
Oh, I see, I had a misunderstanding.
Thanks!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 11:01:27 2024 UTC