-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Currently, argument unpacking and named arguments cannot be mixed at all. This relaxes the restriction to allow foo(...$args, named: $arg). The variant foo(named: $arg, ...$args) is still forbidden, because we can't ensure that positional parameters come before named parameters in that case (without more intrusive changes). Effectively this just enforces a required style, as the order of unpack and named args doesn't matter for the cases where both could be well-defined. ML discussion: https://externals.io/message/114589 Closes GH-7009.
- Loading branch information
Showing
4 changed files
with
58 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,58 @@ | ||
--TEST-- | ||
Mixing unpacking and named params (1) | ||
Named args after unpacking (supported) | ||
--FILE-- | ||
<?php | ||
|
||
test(...[], a: 42); | ||
function test(...$args) { | ||
var_dump($args); | ||
} | ||
|
||
test(...[1, 2], a: 3); | ||
test(...[1, 'a' => 2], b: 3); | ||
|
||
function test2($a, $b, $c = 3, $d = 4) { | ||
var_dump($a, $b, $c, $d); | ||
} | ||
|
||
test2(...[1, 2], d: 40); | ||
test2(...['b' => 2, 'a' => 1], d: 40); | ||
|
||
try { | ||
test2(...[1, 2], b: 20); | ||
} catch (Error $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
try { | ||
test2(...[1, 'b' => 2], b: 20); | ||
} catch (Error $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot combine named arguments and argument unpacking in %s on line %d | ||
--EXPECT-- | ||
array(3) { | ||
[0]=> | ||
int(1) | ||
[1]=> | ||
int(2) | ||
["a"]=> | ||
int(3) | ||
} | ||
array(3) { | ||
[0]=> | ||
int(1) | ||
["a"]=> | ||
int(2) | ||
["b"]=> | ||
int(3) | ||
} | ||
int(1) | ||
int(2) | ||
int(3) | ||
int(40) | ||
int(1) | ||
int(2) | ||
int(3) | ||
int(40) | ||
Named parameter $b overwrites previous argument | ||
Named parameter $b overwrites previous argument |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
--TEST-- | ||
Mixing unpacking and named params (2) | ||
Named args before unpacking (not supported) | ||
--FILE-- | ||
<?php | ||
|
||
test(a: 42, ...[]); | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot combine named arguments and argument unpacking in %s on line %d | ||
Fatal error: Cannot use argument unpacking after named arguments in %s on line %d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters