rfc:typecast_array_desctructuring

PHP RFC: Type casting in array destructuring expressions

Introduction

Adds the possibility to cast values while using array or list destructuring expressions.

Proposal

While a simple variable assignment is able to execute a type cast this possibility isn't present for array or list destructuring expressions. Instead all values are assigned as they are:

// simple assignment with a cast
$now = (int) "2020";
// array destructuring without cast
[$now, $future] = ["2020", "2021"];

If the values stored in $a and $b after the array destructuring shall be casted each value must be casted manually afterwards:

[$now, $future] = ["2020", "2021"];
// either touch each value manually, use array_map or any other implementation approach to cast the values
$now = (int) $now;
$future = (int) $future;

This RFC proposes the possibility to cast the values inside the destructuring expression:

// destructuring and casting of a numeric array
[(int) $now, (int) $future] = ["2020", "2021"];
 
// destructuring and casting of an assiciative array
["now" => (int) $now, "future" => (int) $future] = ["now" => "2020", "future" => "2021"];
 
// destructuring and casting of a nested array
[
    "2020s" => [
        "now" => (int) $now,
        "future" => (int) $future
    ]
] = [
    "2020s" => [
        "now" => "2020",
        "future" => "2021"
    ],
    "2030s" => [
        "far away" => "2039"
    ]
];
 
// destructuring and casting in a foreach loop
$years = [["now", "2020"], ["future", "2021"]];
foreach ($years as [$description, (int) $year]) {
    // ...
}

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:

// destructuring and casting with various types
["address" => (bool) $hasAddress, "floor" => (int) $floor] = ["address" => "My adress", "floor" => "3"];

All of the examples above also work with the list() syntax.

Backward Incompatible Changes

None

Proposed PHP Version(s)

Next PHP version (target 8.0)

RFC Impact

To SAPIs

None

To Existing Extensions

None

To Opcache

Implementation uses existing functions to compile the code. So existing Opcache implementations for assignments and castings are used.

Open Issues

Future Scope

Future scopes may include type casts during reference assignments:

// simple reference assignment cast
$now = "2020";
$now2 = (int) &$now;
 
// reference assignment cast combined with array destructuring
$years = ["2020", "2021"];
[(int) &$now, (int) &$future] = $years;

Future scopes may include strict type casts:

// simple strict assignment cast
$now = "2020";
$now2 = (!int) $now;
 
// simple strict assignment cast combined with array destructuring
$years = ["2020", "2021"];
[(!int) &$now, (!int) &$future] = $years;

Proposed Voting Choices

Vote will require 2/3 majority.

Patches and Tests

Implementation

After the project is implemented, this section should contain

  1. the version(s) it was merged into
  2. a link to the git commit(s)
  3. a link to the PHP manual entry for the feature
  4. a link to the language specification section (if any)

References

Links to external references, discussions or RFCs

Rejected Features

Keep this updated with features that were discussed on the mail lists.

rfc/typecast_array_desctructuring.txt · Last modified: 2020/03/26 12:52 by wol-soft