rfc:typecast_array_desctructuring
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:typecast_array_desctructuring [2020/03/26 12:50] – Added example to cast with various types wol-soft | rfc:typecast_array_desctructuring [2020/04/09 09:32] – Start voting wol-soft | ||
---|---|---|---|
Line 2: | Line 2: | ||
* Version: 0.2 | * 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: | + | * Status: |
- | * First Published at: http://wiki.php.net/rfc/typecast_array_desctructuring | + | * Implementation: https://github.com/php/php-src/ |
===== 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: | ||
+ | |||
+ | < | ||
+ | 1,Test,2002 | ||
+ | 2, | ||
+ | 3,Demo,2016 | ||
+ | </ | ||
+ | |||
+ | <code php> | ||
+ | $handle = fopen(' | ||
+ | while (($data = fgetcsv($handle)) !== false) { | ||
+ | [(int) $id, (string) $data, (int) $year] = $data; | ||
+ | // ... | ||
+ | } | ||
+ | </ | ||
===== Proposal ===== | ===== Proposal ===== | ||
Line 60: | Line 79: | ||
</ | </ | ||
- | 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 sould cover various types: | + | 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 |
<code php> | <code php> | ||
Line 91: | 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 = [" | ||
+ | [int $now, int $future] = $years; | ||
+ | </ | ||
+ | |||
+ | 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' | ||
===== Future Scope ===== | ===== Future Scope ===== | ||
- | Future scopes may include type casts during reference assignments: | + | Future scopes may include type casts during reference assignments |
<code php> | <code php> | ||
- | // simple | + | // reference assignment cast |
$now = " | $now = " | ||
$now2 = (int) &$now; | $now2 = (int) &$now; | ||
Line 106: | Line 139: | ||
</ | </ | ||
- | Future scopes may include strict type casts: | + | ---- |
+ | |||
+ | Future scopes may include strict type casts which avoid eg. (!int) "No Number String" | ||
<code php> | <code php> | ||
- | // simple | + | // strict assignment cast |
$now = " | $now = " | ||
$now2 = (!int) $now; | $now2 = (!int) $now; | ||
- | // simple | + | // strict assignment cast combined with array destructuring |
$years = [" | $years = [" | ||
- | [(!int) | + | [(!int) $now, (!int) |
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | Future scopes may include nullable type casts (compare https:// | ||
+ | |||
+ | <code php> | ||
+ | // nullable assignment cast | ||
+ | $now = " | ||
+ | $now2 = (?int) $now; | ||
+ | |||
+ | // nullable assignment cast combined with array destructuring | ||
+ | $years = [" | ||
+ | [(?int) $now, (?int) $future, (?int) $evenLater] = $years; | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | Future scopes may include regular type checks which depend on strict_types directive: | ||
+ | |||
+ | <code php> | ||
+ | $years = [" | ||
+ | [int $now, int $future] = $years; | ||
</ | </ | ||
===== Proposed Voting Choices ===== | ===== Proposed Voting Choices ===== | ||
- | Vote will require | + | Voting starts 2020-04-09 and ends 2020-04-23. |
+ | |||
+ | As this is a language change, a 2/3 majority | ||
+ | |||
+ | <doodle title=" | ||
+ | * yes | ||
+ | * no | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | 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=" | ||
+ | * reference assignment casts | ||
+ | * strict casts | ||
+ | * nullable casts | ||
+ | * type checks in array destructuring expressions | ||
+ | * none | ||
+ | </ | ||
===== 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:// | https:// |
rfc/typecast_array_desctructuring.txt · Last modified: 2020/04/23 08:40 by wol-soft