rfc:auto-capture-lambda
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:auto-capture-lambda [2021/03/22 21:57] – crell | rfc:auto-capture-lambda [2021/03/24 15:47] – Comment. crell | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== PHP RFC: Auto-capturing | + | ====== PHP RFC: Auto-capturing |
* Version: 0.9 | * Version: 0.9 | ||
* Date: 2021-03-22 | * Date: 2021-03-22 | ||
Line 8: | Line 8: | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | Lambdas | + | Closures (also known as lambdas or anonymous functions), |
- | Specifically, | + | <code php> |
+ | // As of 8.0: | ||
+ | |||
+ | $y = 1; | ||
+ | |||
+ | $fn1 = fn($x) => $x + $y; // auto-capture + single expression | ||
+ | |||
+ | $fn2 = function ($x) use ($y): int { // manual-capture + statement list | ||
+ | // ... | ||
+ | |||
+ | | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | The proposed syntax combines the auto-capture and multi-line capabilities into a single syntax: | ||
+ | |||
+ | <code php> | ||
+ | $fn3 = fn ($x): int { // auto-capture + statement list | ||
+ | // ... | ||
+ | |||
+ | return $x + $y; | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | This RFC has also been designed in concert with the [[rfc: | ||
===== Proposal ===== | ===== Proposal ===== | ||
Line 21: | Line 45: | ||
// A named, globally available function. | // A named, globally available function. | ||
- | // No values | + | // No variables |
// The body is a statement list, with possibly a return statement. | // The body is a statement list, with possibly a return statement. | ||
function foo($a, $b, $c): int { | function foo($a, $b, $c): int { | ||
Line 28: | Line 52: | ||
// An anonymous, locally available function. | // An anonymous, locally available function. | ||
- | // Values | + | // Variables |
// The body is a statement list, with possibly a return statement. | // The body is a statement list, with possibly a return statement. | ||
$foo = function ($a, $b) use ($c) { | $foo = function ($a, $b) use ($c) { | ||
Line 35: | Line 59: | ||
// An anonymous, locally available function. | // An anonymous, locally available function. | ||
- | // Values | + | // Variables |
// The body is a single-expression, | // The body is a single-expression, | ||
$foo = fn($a, $b): int => $a * $b * $c; | $foo = fn($a, $b): int => $a * $b * $c; | ||
Line 44: | Line 68: | ||
The [[rfc: | The [[rfc: | ||
- | This RFC seeks to add a different combination: | + | This RFC seeks to add a different combination: |
The remaining combinations would be: | The remaining combinations would be: | ||
Line 50: | Line 74: | ||
* named function, auto-capture, | * named function, auto-capture, | ||
* named function, auto-capture, | * named function, auto-capture, | ||
- | * anonymous function, manual-capture, | + | * anonymous function, manual-capture, |
- | ==== Auto-capture multi-statement | + | ==== Auto-capture multi-statement |
Specifically, | Specifically, | ||
Line 58: | Line 82: | ||
<code php> | <code php> | ||
// An anonymous, locally available function. | // An anonymous, locally available function. | ||
- | // Values | + | // Variables |
- | // The body is a series of statements, potentially ending in a return statement; | + | // The body is a statement list, with possibly |
$c = 1; | $c = 1; | ||
- | $foo = fn($a, $b) { | + | $foo = fn($a, $b):int { |
$val = $a * $b; | $val = $a * $b; | ||
return $val * $c; | return $val * $c; | ||
Line 67: | Line 91: | ||
</ | </ | ||
- | The syntax choice here, in combination with short-functions, | + | The syntax choice here, in combination with the short-functions |
* The '' | * The '' | ||
* '' | * '' | ||
* The '' | * The '' | ||
- | * The '' | + | * The '' |
- | * A function with a name is declared globally at compile time. A function without a name is declared locally as a lambda | + | * A function with a name is declared globally at compile time. A function without a name is declared locally as a closure |
These rules are easily recognizable and learnable by developers. | These rules are easily recognizable and learnable by developers. | ||
Line 81: | Line 105: | ||
As methods cannot be anonymous, there are no impacts on methods from this RFC. The short-functions RFC does address methods, and does so in a way that is completely consistent with the syntactic rules defined above. | As methods cannot be anonymous, there are no impacts on methods from this RFC. The short-functions RFC does address methods, and does so in a way that is completely consistent with the syntactic rules defined above. | ||
- | ==== What about long-lambdas? ==== | + | ==== What about long-closures? ==== |
- | The existing multi-line | + | The existing multi-line |
* When it is desirable to capture variables explicitly, such as to avoid name collision. | * When it is desirable to capture variables explicitly, such as to avoid name collision. | ||
* When it is desirable to capture a variable by reference. | * When it is desirable to capture a variable by reference. | ||
+ | |||
+ | <code php> | ||
+ | // This remains the only way to capture by reference. | ||
+ | $c = 1; | ||
+ | $f = function($a, | ||
+ | $c = $a * $b; | ||
+ | }; | ||
+ | </ | ||
==== Multi-line expressions ==== | ==== Multi-line expressions ==== | ||
Line 120: | Line 152: | ||
===== Open Issues ===== | ===== Open Issues ===== | ||
- | Make sure there are no open issues when the vote starts! | + | |
+ | None. | ||
===== Unaffected PHP Functionality ===== | ===== Unaffected PHP Functionality ===== | ||
Line 133: | Line 166: | ||
<code php> | <code php> | ||
- | $GLOBALS[' | + | // Global scope |
+ | $c = 1; | ||
- | fn foo($a, $b) { | + | fn foo($a, $b): int { |
$val = $a * $b; | $val = $a * $b; | ||
return $val * $c; | return $val * $c; | ||
} | } | ||
- | fn foo($a, $b) => $a * $b * $c; | + | fn foo($a, $b): int => $a * $b * $c; |
- | $foo = fn($a, $b) use ($c) => $a * $b * $c; | + | $foo = fn($a, $b) use ($c): int => $a * $b * $c; |
</ | </ | ||
Line 153: | Line 187: | ||
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
- | Patch is in progress from Nuno. It will be included prior to the official start of discussion. | + | Pull Request: https:// |
===== Implementation ===== | ===== Implementation ===== |
rfc/auto-capture-lambda.txt · Last modified: 2021/03/24 15:58 by crell