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
rfc:typecast_array_desctructuring [2020/03/25 17:46] – created wol-softrfc:typecast_array_desctructuring [2020/04/23 08:40] (current) 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: Declined 
-  * First Published athttp://wiki.php.net/rfc/typecast_array_desctructuring+  * Implementationhttps://github.com/php/php-src/pull/5296
  
 ===== 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>
-// simple reference assignment cast+// reference assignment cast
 $now = "2020"; $now = "2020";
 $now2 = (int) &$now; $now2 = (int) &$now;
Line 100: Line 139:
 </code> </code>
  
-Future scopes may include strict type casts:+---- 
 + 
 +Future scopes may include strict type casts which avoid eg. (!int) "No Number String" to be casted to 0:
  
 <code php> <code php>
-// simple strict assignment cast+// strict assignment cast
 $now = "2020"; $now = "2020";
 $now2 = (!int) $now; $now2 = (!int) $now;
  
-// simple strict assignment cast combined with array destructuring+// 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 nullable type casts (compare https://wiki.php.net/rfc/nullable-casting): 
 + 
 +<code php> 
 +// nullable assignment cast 
 +$now = "2020"; 
 +$now2 = (?int) $now; 
 + 
 +// nullable assignment cast combined with array destructuring 
 +$years = ["2020", "2021", null]; 
 +[(?int) $now, (?int) $future, (?int) $evenLater] = $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>
  
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
  
-Vote will require 2/3 majority.+Voting starts 2020-04-09 and ends 2020-04-23. 
 + 
 +As this is a language change, a 2/3 majority is requiredThe vote is a straight Yes/No vote for accepting the RFC and merging the patch. 
 + 
 +<doodle title="Add type casting in array destructuring expressions" auth="wol-soft" voteType="single" closed="true"> 
 +   * yes 
 +   * no 
 +</doodle> 
 + 
 +---- 
 + 
 +As the future scopes section of this proposal includes a lot of possible topics an additional poll to see which of these topics may be tackled in the near future: 
 + 
 +<doodle title="Choose one or more of the suggested future scopes in which you are interested" auth="wol-soft" voteType="multi" closed="true"> 
 +   * reference assignment casts 
 +   * strict casts 
 +   * nullable casts 
 +   * type checks in array destructuring expressions 
 +   * none 
 +</doodle>
  
 ===== 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.1585158391.txt.gz · Last modified: 2020/03/25 17:46 by wol-soft