rfc:typecast_array_desctructuring

Differences

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

Link to this comparison view

Next revision
Previous revision
Next revisionBoth sides next revision
rfc:typecast_array_desctructuring [2020/03/25 17:46] – created wol-softrfc:typecast_array_desctructuring [2020/04/08 09:26] – Added motivation and discussion section wol-soft
Line 1: Line 1:
 ====== PHP RFC: Type casting in array destructuring expressions ====== ====== PHP RFC: Type casting in array destructuring expressions ======
-  * Version: 0.1+  * Version: 0.2
   * Date: 2020-03-25   * Date: 2020-03-25
 +  * Target Version: PHP 8.0
   * Author: Enno Woortmann, enno.woortmann@web.de   * Author: Enno Woortmann, enno.woortmann@web.de
   * Status: Under Discussion   * Status: Under Discussion
Line 8: Line 9:
 ===== Introduction ===== ===== Introduction =====
 Adds the possibility to cast values while using array or list destructuring expressions. Adds the possibility to cast values while using array or list destructuring expressions.
 +
 +===== Motivation =====
 +
 +This proposal aims at type casts when working with data from a untyped source (eg. iterating over a CSV file, XML without a XSD defining the types of the elements). This data can often be handled in an elegant way using the existing capabilities of array destructuring. While simple variable assignments can be casted during the assignment this isn't possible during assignments via array destructuring. This proposal adds a new syntax for casting in array assignments. An example when handling a CSV file may look like:
 +
 +<code>
 +1,Test,2002
 +2,Example,2010
 +3,Demo,2016
 +</code>
 +
 +<code php>
 +$handle = fopen('test.csv', 'r');
 +while (($data = fgetcsv($handle)) !== false) {
 +    [(int) $id, (string) $data, (int) $year] = $data;
 +    // ...
 +}
 +</code>
  
 ===== Proposal ===== ===== Proposal =====
Line 60: Line 79:
 </code> </code>
  
 +While examples where all values should be casted to the identical type may be solved reasonably elegant with solutions like array_map it get's more difficult if the casts should cover various types:
 +
 +<code php>
 +// destructuring and casting with various types
 +["address" => (bool) $hasAddress, "floor" => (int) $floor] = ["address" => "My adress", "floor" => "3"];
 +</code>
  
 All of the examples above also work with the list() syntax. All of the examples above also work with the list() syntax.
Line 85: Line 110:
  
 ===== Open Issues ===== ===== Open Issues =====
 +
 +
 +===== Discussion =====
 +
 +Regular type checks
 +
 +During the discussion especially the idea of regular type checks instead of castings came up:
 +
 +<code php>
 +$years = ["2020", "2021"];
 +[int $now, int $future] = $years;
 +</code>
 +
 +As a regular type check depends on the declare strict_types directive concerning the casting feature (strict_types=0 would result in an implicit cast similar to the proposed feature while strict_types=1 would result in a type error when the provided data doesn't match the type check) a regular type check covers different use cases than the proposed casting feature. (Also see future scopes)
  
 ===== Future Scope ===== ===== Future Scope =====
  
-Future scopes may include type casts during reference assignments:+Future scopes may include type casts during reference assignments which lead to a cast of the referenced variable (compare https://wiki.php.net/rfc/list_reference_assignment for reference assignments without casts):
  
 <code php> <code php>
Line 109: Line 148:
 // simple strict assignment cast combined with array destructuring // simple strict assignment cast combined with array destructuring
 $years = ["2020", "2021"]; $years = ["2020", "2021"];
-[(!int) &$now, (!int) &$future] = $years;+[(!int) $now, (!int) $future] = $years; 
 +</code> 
 + 
 +Future scopes may include regular type checks which depend on strict_types directive: 
 + 
 +<code php> 
 +$years = ["2020", "2021"]; 
 +[int $now, int $future] = $years;
 </code> </code>
  
Line 117: Line 163:
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
 +
 +The parser is already able to parse the syntax and requires no changes. The patch adds a change in the compile process in zend_compile_list_assign to be able to handle casting AST elements.
  
 https://github.com/php/php-src/pull/5296 https://github.com/php/php-src/pull/5296
rfc/typecast_array_desctructuring.txt · Last modified: 2020/04/23 08:40 by wol-soft