rfc:typehint_array_desctructuring

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:typehint_array_desctructuring [2020/04/16 10:00] wol-softrfc:typehint_array_desctructuring [2020/04/16 13:56] (current) wol-soft
Line 1: Line 1:
-====== PHP RFC: Type hints in array destructuring expressions ======+====== PHP RFC: Type declarations in array destructuring expressions ======
   * Version: 0.1   * Version: 0.1
   * Date: 2020-04-16   * Date: 2020-04-16
Line 7: Line 7:
  
 ===== Introduction ===== ===== Introduction =====
-Adds the possibility to type hint values while using array or list destructuring expressions.+Adds the possibility to extend values with type declarations while using array or list destructuring expressions.
  
 ===== Motivation ===== ===== Motivation =====
  
-When working with array destructuring expressions it's currently not possible to type hint the values from the array being destructured. As the destructured data often comes from various sources which may or may not be controlled by the scope executing the destructuring, a type hint inside the expression allows stricter boundaries for the processed array:+When working with array destructuring expressions it's currently not possible to extend the values from the array being destructured with type declarations. As the destructured data often comes from various sources which may or may not be controlled by the scope executing the destructuring, a type declaration inside the expression allows stricter boundaries for the processed array:
  
 <code php> <code php>
Line 19: Line 19:
  
 ===== Proposal ===== ===== Proposal =====
-This proposal adds a new syntax to add type hints to array destructuring expressions. The type hints behave identically to the type hints processed for function calls (compare https://wiki.php.net/rfc/scalar_type_hints_v5). This includes a controllability of the behaviour using the strict_types directive:+This proposal adds a new syntax to add type declarations to array destructuring expressions. The type declarations behave identically to the type declarations processed for function calls (compare https://wiki.php.net/rfc/scalar_type_hints_v5). This includes a controllability of the behaviour using the strict_types directive. Type checks will be executed during the assignment of the variables when resolving the array destructuring expression. The variables will **not** keep any information concerning the check (they will not behave like typed variables afterwards, just like type checks in method signatures).
  
 <code php> <code php>
Line 37: Line 37:
  
 <code php> <code php>
-// type hinting an assiciative array destructuring+// assiciative array destructuring
 ["now" => int $now, "future" => int $future] = ["now" => 2020, "future" => 2021]; ["now" => int $now, "future" => int $future] = ["now" => 2020, "future" => 2021];
  
-// type hinting a nested array destructuring+// nested array destructuring
 [ [
     "2020s" => [     "2020s" => [
Line 56: Line 56:
 ]; ];
  
-// type hinting in a foreach loop+// destructuring in a foreach loop
 $years = [["now", 2020], ["future", 2021]]; $years = [["now", 2020], ["future", 2021]];
 foreach ($years as [string $description, int $year]) { foreach ($years as [string $description, int $year]) {
Line 63: Line 63:
 </code> </code>
  
-Additionally to the examples above which all use scalar type hints also object type hints are possible:+Additionally to the examples above which all use scalar type declarations also object type declarations are possible:
  
 <code php> <code php>
Line 69: Line 69:
     // ...     // ...
 } }
 +</code>
 +
 +The for PHP 8.0 accepted union types (https://wiki.php.net/rfc/union_types_v2) will also be allowed:
 +
 +<code php>
 +[int|float $number, string $description] = [1.5, "One point five"]
 </code> </code>
  
 All of the examples above also work with the list() syntax. All of the examples above also work with the list() syntax.
 +
 +===== Future scope =====
 +
 +Future scopes may include adding type checks to any assignment:
 +
 +<code php>
 +int $id = $data['id'];
 +</code>
 +
 +----
 +
 +Future scopes may include adding type declarations to foreach loops not utilizing array destructuring (https://externals.io/message/104485#104488):
 +
 +<code php>
 +$years = ["now" => 2020, "future" => 2021];
 +foreach ($years as string $description => int $year) {
 +    // ...
 +}
 +</code>
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
rfc/typehint_array_desctructuring.1587031240.txt.gz · Last modified: 2020/04/16 10:00 by wol-soft