rfc:closures_in_const_expr

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
rfc:closures_in_const_expr [2024/10/29 15:03] – Under Discussion timwollarfc:closures_in_const_expr [2024/12/02 17:27] (current) – Implemented timwolla
Line 3: Line 3:
   * Date: 2024-10-24   * Date: 2024-10-24
   * Author: Tim Düsterhus (tim@tideways-gmbh.com), Volker Dusch (volker@tideways-gmbh.com)   * Author: Tim Düsterhus (tim@tideways-gmbh.com), Volker Dusch (volker@tideways-gmbh.com)
-  * Status: Under Discussion+  * Status: Implemented 
 +  * Target Version: PHP 8.5 
 +  * Implementation: https://github.com/php/php-src/commit/f6a0bb4d0442d2780c61d32d01c117024eccab57
   * First Published at: https://wiki.php.net/rfc/closures_in_const_expr   * First Published at: https://wiki.php.net/rfc/closures_in_const_expr
  
Line 45: Line 47:
  
   * Attribute parameters.   * Attribute parameters.
-  * Default values of properties, parameters, and static variables.+  * Default values of properties and parameters.
   * Constants and Class Constants.   * Constants and Class Constants.
  
Line 52: Line 54:
 If Closures are placed in constant expressions they are subject to the following constraints: If Closures are placed in constant expressions they are subject to the following constraints:
  
-  * They may not include variables from the surrounding scope using <php>use($foo, $bar)</php>, because except for constants and static variables there is no surrounding scope. This also means that short closures (arrow functions) are not supported, because they perform implicit capturing. This constraint is consistent with how variables may not be part of a constant expression. +  * They may not include variables from the surrounding scope using <php>use($foo, $bar)</php>, because except for constants and possibly for default parameter values there is no surrounding scope. This also means that short closures (arrow functions) are not supported, because they perform implicit capturing. This constraint is consistent with how variables may not be part of a constant expression. 
-  * They must be <php>static</php> (and thus they must not access <php>$this</php>). Semantically <php>$this</php> would only be well-defined for property default values and possibly attribute parameters, but this would require reevaluating the Closure for each object / attribute instance, which would be different to existing constant expressions which are only evaluated once. This constraint is consistent with how <php>new</php> expressions may not be used in property default values.+  * They must be <php>static</php> (and thus they must not access <php>$this</php>). Semantically <php>$this</php> would only be well-defined for property default values and possibly attribute parameters, but this would require reevaluating the Closure for each object / attribute instance, which would be different to existing constant expressions which are only evaluated once. This constraint is consistent with how <php>new</php>-expressions may not be used in property default values.
  
 Both of these constraints will be verified at compile time. Both of these constraints will be verified at compile time.
Line 92: Line 94:
 </PHP> </PHP>
  
-Passing a Closure to a new expression:+Passing a Closure to a <php>new</php>-expression:
  
 <PHP> <PHP>
Line 198: Line 200:
   * Support non-static Closures.   * Support non-static Closures.
   * Support first-class callables.   * Support first-class callables.
 +  * Support variable capturing if/when variables may appear in constant expressions.
  
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
  
-<doodle title="Support Closures in constant expressions as proposed?" auth="timwolla" voteType="single" closed="true" closeon="2024-06-05T08:00:00Z">+<doodle title="Support Closures in constant expressions as proposed?" auth="timwolla" voteType="single" closed="true" closeon="2024-11-27T14:00:00Z">
    * Yes    * Yes
    * No    * No
Line 212: Line 215:
 ===== Implementation ===== ===== Implementation =====
  
-n/a+https://github.com/php/php-src/commit/f6a0bb4d0442d2780c61d32d01c117024eccab57
  
 ===== References ===== ===== References =====
Line 219: Line 222:
   * https://github.com/php/php-src/pull/16458   * https://github.com/php/php-src/pull/16458
   * [[rfc:new_in_initializers|RFC: New in initializers]]   * [[rfc:new_in_initializers|RFC: New in initializers]]
 +  * https://github.com/php/php-src/issues/12139
  
 ===== Rejected Features ===== ===== Rejected Features =====
  
 n/a n/a
rfc/closures_in_const_expr.1730214235.txt.gz · Last modified: 2024/10/29 15:03 by timwolla