rfc:auto-capture-lambda

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
rfc:auto-capture-lambda [2021/03/24 15:31] – Updates from Nuno crellrfc:auto-capture-lambda [2021/03/24 15:54] crell
Line 1: Line 1:
-====== PHP RFC: Auto-capturing multi-line lambdas ======+====== PHP RFC: Auto-capturing multi-statement closures ======
   * Version: 0.9   * Version: 0.9
   * Date: 2021-03-22   * Date: 2021-03-22
Line 8: Line 8:
 ===== Introduction ===== ===== Introduction =====
  
-Lambdas have become increasingly powerful and useful in PHP in recent versions.  In their current form they have two versions, long and short.  Unfortunately, these two syntaxes have different, mutually-incompatible benefits.  This RFC proposes a syntax for lambdas that combines the benefits of both for those situations where that is warranted.+Closures (also known as lambdas or anonymous functions), have become increasingly powerful and useful in PHP in recent versions.  In their current form they have two versions, long and short.  Unfortunately, these two syntaxes have different, mutually-incompatible benefits.  This RFC proposes a syntax for closures that combines the benefits of both for those situations where that is warranted.
  
 <code php> <code php>
 +// As of 8.0:
 +
 $y = 1; $y = 1;
  
Line 72: Line 74:
   * named function, auto-capture, statement list - This is of little use in practice as there is nothing to auto-capture, except potentially global variables.   * named function, auto-capture, statement list - This is of little use in practice as there is nothing to auto-capture, except potentially global variables.
   * named function, auto-capture, expression - Ibid.   * named function, auto-capture, expression - Ibid.
-  * anonymous function, manual-capture, expression - While this form would be possible to add, its use cases are limited.  The existing short-lambda syntax is superior in nearly all cases.+  * anonymous function, manual-capture, expression - While this form would be possible to add, its use cases are limited.  The existing short-closure syntax is superior in nearly all cases.
  
-==== Auto-capture multi-statement lambdas ====+==== Auto-capture multi-statement closures ====
  
 Specifically, this RFC adds the following syntax: Specifically, this RFC adds the following syntax:
Line 95: Line 97:
   * The ''function'' keyword indicates a function that has no auto-capture.   * The ''function'' keyword indicates a function that has no auto-capture.
   * The ''fn'' keyword indicates a function that will auto-capture variables, by value.   * The ''fn'' keyword indicates a function that will auto-capture variables, by value.
-  * A function with a name is declared globally at compile time.  A function without a name is declared locally as a lambda at runtime.+  * A function with a name is declared globally at compile time.  A function without a name is declared locally as a closure at runtime.
  
 These rules are easily recognizable and learnable by developers. These rules are easily recognizable and learnable by developers.
Line 103: 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 lambda syntax remains valid, and there is no intent to deprecate it.  It is likely to become less common in practice, but it still has two use cases where it will be necessary:+The existing multi-line closure syntax remains valid, and there is no intent to deprecate it.  It is likely to become less common in practice, but it still has two use cases where it will be necessary:
  
   * 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.
rfc/auto-capture-lambda.txt · Last modified: 2021/03/24 15:58 by crell