-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Implement short functions with auto capture #8330
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9dec265
Short closures with minimal auto capture
arnaud-lb c87d3c0
Use CG(arena)
arnaud-lb 534c698
Add tests
arnaud-lb aca1f54
Add test
arnaud-lb a24dbeb
Add test
arnaud-lb 1d13cb0
Sync with RFC: arrow functions are not changed
arnaud-lb d032b5e
Sync with RFC: no explicit use list
arnaud-lb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
--TEST-- | ||
Short closures | ||
--FILE-- | ||
<?php | ||
|
||
$a = 1; | ||
$b = 2; | ||
|
||
$f = fn ($x) { | ||
return [$a, $b, $x]; | ||
}; | ||
|
||
echo "Captures:\n"; | ||
var_dump((new ReflectionFunction($f))->getStaticVariables()); | ||
|
||
echo "Result:\n"; | ||
var_dump($f(3)); | ||
--EXPECT-- | ||
Captures: | ||
array(2) { | ||
["a"]=> | ||
int(1) | ||
["b"]=> | ||
int(2) | ||
} | ||
Result: | ||
array(3) { | ||
[0]=> | ||
int(1) | ||
[1]=> | ||
int(2) | ||
[2]=> | ||
int(3) | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
--TEST-- | ||
Short closures syntax variations | ||
--FILE-- | ||
<?php | ||
|
||
$a = 1; | ||
|
||
function printFunctionSignature($f) { | ||
$rf = new ReflectionFunction($f); | ||
if ($rf->isStatic()) { | ||
print 'static '; | ||
} | ||
print 'fn '; | ||
if ($rf->returnsReference()) { | ||
print '& '; | ||
} | ||
print '(...)'; | ||
$usedVars = $rf->getClosureUsedVariables(); | ||
if (count($usedVars) > 0) { | ||
print ' use ('; | ||
$n = 0; | ||
foreach ($usedVars as $var => $_) { | ||
if ($n++ > 0) { | ||
print ', '; | ||
} | ||
print $var; | ||
} | ||
print ')'; | ||
} | ||
$type = $rf->getReturnType(); | ||
if ($type !== null) { | ||
print ': ' . $type->getName(); | ||
} | ||
print "\n"; | ||
}; | ||
|
||
$f = fn () { | ||
return 1; | ||
}; | ||
|
||
printFunctionSignature($f); | ||
|
||
$f = fn & () { | ||
return 1; | ||
}; | ||
|
||
printFunctionSignature($f); | ||
|
||
$f = static fn () { | ||
return 1; | ||
}; | ||
|
||
printFunctionSignature($f); | ||
|
||
$f = fn (): Foo { | ||
return 1; | ||
}; | ||
|
||
printFunctionSignature($f); | ||
|
||
$f = fn () use ($a) { | ||
}; | ||
|
||
printFunctionSignature($f); | ||
|
||
$f = fn () use ($a): Foo { | ||
}; | ||
|
||
printFunctionSignature($f); | ||
|
||
--EXPECTF-- | ||
fn (...) | ||
fn & (...) | ||
static fn (...) | ||
fn (...): Foo | ||
fn (...) use (a) | ||
fn (...) use (a): Foo | ||
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--TEST-- | ||
Short closures: explicit use | ||
--FILE-- | ||
<?php | ||
|
||
$a = 1; | ||
$b = 2; | ||
|
||
$f = fn ($x) use ($a, &$b) { | ||
$b = 4; | ||
return [$a, $b, $x]; | ||
}; | ||
|
||
echo "Captures:\n"; | ||
var_dump((new ReflectionFunction($f))->getStaticVariables()); | ||
|
||
echo "Result:\n"; | ||
var_dump($f(3)); | ||
|
||
echo "\$b:\n"; | ||
var_dump($b); | ||
--EXPECT-- | ||
Captures: | ||
array(2) { | ||
["a"]=> | ||
int(1) | ||
["b"]=> | ||
&int(2) | ||
} | ||
Result: | ||
array(3) { | ||
[0]=> | ||
int(1) | ||
[1]=> | ||
int(4) | ||
[2]=> | ||
int(3) | ||
} | ||
$b: | ||
int(4) |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--TEST-- | ||
Short closures minimal capture: simple case | ||
--FILE-- | ||
<?php | ||
|
||
$a = 1; | ||
$x = 1; | ||
|
||
$f = fn () { | ||
$x = 2; | ||
return $x + $a; | ||
}; | ||
|
||
echo "Captures:\n"; | ||
var_dump((new ReflectionFunction($f))->getStaticVariables()); | ||
|
||
echo "f():\n"; | ||
var_dump($f()); | ||
--EXPECT-- | ||
Captures: | ||
array(1) { | ||
["a"]=> | ||
int(1) | ||
} | ||
f(): | ||
int(3) |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
--TEST-- | ||
Short closures minimal capture: loop | ||
--FILE-- | ||
<?php | ||
|
||
$a = -1; | ||
$b = 2; | ||
|
||
// Captures $a because it may be used before definition | ||
$f = fn ($x) { | ||
for ($i = 0; $i < $x; $i++) { | ||
$a = $i; | ||
} | ||
return $a; | ||
}; | ||
|
||
echo "Captures:\n"; | ||
var_dump((new ReflectionFunction($f))->getStaticVariables()); | ||
|
||
echo "f(3):\n"; | ||
var_dump($f(3)); | ||
|
||
echo "f(0):\n"; | ||
var_dump($f(0)); | ||
--EXPECT-- | ||
Captures: | ||
array(1) { | ||
["a"]=> | ||
int(-1) | ||
} | ||
f(3): | ||
int(2) | ||
f(0): | ||
int(-1) |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--TEST-- | ||
Short closures minimal capture: goto | ||
--FILE-- | ||
<?php | ||
|
||
$a = -1; | ||
$b = 2; | ||
|
||
// Does not capture $a because it's always defined | ||
$f = fn ($x) { | ||
goto end; | ||
|
||
ret: | ||
return $a + $x; | ||
|
||
end: | ||
$a = $b; | ||
goto ret; | ||
}; | ||
|
||
echo "Captures:\n"; | ||
var_dump((new ReflectionFunction($f))->getStaticVariables()); | ||
|
||
echo "f(3):\n"; | ||
var_dump($f(3)); | ||
|
||
echo "f(0):\n"; | ||
var_dump($f(0)); | ||
--EXPECT-- | ||
Captures: | ||
array(1) { | ||
["b"]=> | ||
int(2) | ||
} | ||
f(3): | ||
int(5) | ||
f(0): | ||
int(2) |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--TEST-- | ||
Short closures minimal capture: static | ||
--FILE-- | ||
<?php | ||
|
||
$a = 1; | ||
$x = 1; | ||
|
||
$f = fn () { | ||
static $a; | ||
$x = 2; | ||
return $x + (int) $a; | ||
}; | ||
|
||
echo "Captures or statics:\n"; | ||
var_dump((new ReflectionFunction($f))->getStaticVariables()); | ||
|
||
echo "f():\n"; | ||
var_dump($f(3)); | ||
arnaud-lb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
--EXPECT-- | ||
Captures or statics: | ||
array(1) { | ||
["a"]=> | ||
NULL | ||
} | ||
f(): | ||
int(2) |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--TEST-- | ||
Short closures minimal capture: assign ref | ||
--FILE-- | ||
<?php | ||
|
||
$a = 1; | ||
$x = 1; | ||
|
||
$f = fn () { | ||
$b = 2; | ||
$x = &$b; | ||
return $x + $a; | ||
}; | ||
|
||
echo "Captures:\n"; | ||
var_dump((new ReflectionFunction($f))->getStaticVariables()); | ||
|
||
echo "f():\n"; | ||
var_dump($f()); | ||
--EXPECT-- | ||
Captures: | ||
array(1) { | ||
["a"]=> | ||
int(1) | ||
} | ||
f(): | ||
int(3) |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--TEST-- | ||
Short closures minimal capture: global | ||
--FILE-- | ||
<?php | ||
|
||
$a = 1; | ||
|
||
$f = fn () { | ||
global $a; | ||
return $a; | ||
}; | ||
|
||
echo "Captures:\n"; | ||
var_dump((new ReflectionFunction($f))->getStaticVariables()); | ||
|
||
echo "f():\n"; | ||
var_dump($f()); | ||
--EXPECT-- | ||
Captures: | ||
array(0) { | ||
} | ||
f(): | ||
int(1) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.