rfc:typehint_array_desctructuring
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
rfc:typehint_array_desctructuring [2020/04/16 11:43] – wol-soft | rfc: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 |
* 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 |
===== 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, | + | When working with array destructuring expressions it's currently not possible to extend |
<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:// | + | This proposal adds a new syntax to add type declarations |
<code php> | <code php> | ||
Line 37: | Line 37: | ||
<code php> | <code php> | ||
- | // type hinting an assiciative array destructuring | + | // assiciative array destructuring |
[" | [" | ||
- | // type hinting a nested array destructuring | + | // nested array destructuring |
[ | [ | ||
" | " | ||
Line 56: | Line 56: | ||
]; | ]; | ||
- | // type hinting | + | // destructuring |
$years = [[" | $years = [[" | ||
foreach ($years as [string $description, | foreach ($years as [string $description, | ||
Line 63: | Line 63: | ||
</ | </ | ||
- | 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 |
<code php> | <code php> | ||
Line 78: | Line 78: | ||
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[' | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | Future scopes may include adding type declarations to foreach loops not utilizing array destructuring (https:// | ||
+ | |||
+ | <code php> | ||
+ | $years = [" | ||
+ | foreach ($years as string $description => int $year) { | ||
+ | // ... | ||
+ | } | ||
+ | </ | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== |
rfc/typehint_array_desctructuring.1587037384.txt.gz · Last modified: 2020/04/16 11:43 by wol-soft