On Thu, Apr 16, 2020 at 12:20 PM Enno Woortmann <enno.woortm...@web.de> wrote: > > Hi together, > > as the voting for the "Type casting in array destructuring expressions" > shows a clear direction to be declined (sad faces on my side, I really > would've liked it as a feature completion of the casting feature set > without the need for a really new syntax, as the parser also already > coverred it, but ok, time to continue :D ), combined with a strong > interest for the topic "type checks in array destructuring expressions" > (as well in the discussion as in the vote) I've set up a first draft of > an RFC covering this topic: > > https://wiki.php.net/rfc/typehint_array_desctructuring > > The discussion around the casting in array destructuring brought up the > idea to perform regular type checks in array destructuring expressions. > This RFC proposes a new syntax to type hint a variable inside an array > destructuring expression: > > $data = [42, 'Example', 2002]; > [int $id, string $data, int $year] = $data; > > The type hints behave identically to the type hints used in method > signatures (compare https://wiki.php.net/rfc/scalar_type_hints_v5). This > especially includes a behaviour depending on the strict_types directive. > > Additionally to scalar type hints also object type hints are possible. > > I've not yet started an implementation draft but I think this one will > be more tricky than the type cast implementation approach. As the parser > re-uses the array definition for array destructuring expressions (which > also leads to the type cast implementation without touching the parser) > adding type hints to the definition would also affect other parts of the > language eg. something like: > > $x = [int $a, string $b]; > > would be parsed by the parser. I'm currently not able to grasp the > impact of such a change. But that's another topic. > > Let's focus on the idea itself instead of the implementation first. > Thoughts on the first draft? > > Cheers, Enno > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php >
Hi, thanks for proposing, I guess it could be useful (or convenient at least). Just 2 thoughts: - It has been requested several times on this list to stop using the wording "type hint" and instead write "type declaration" ;) - When I see this example in the RFC: ``` $years = [["now", 2020], ["future", 2021]]; foreach ($years as [string $description, int $year]) { ``` I immediately think of a similar: ``` $years = ["now" => 2020, "future" => 2021]; foreach ($years as string $description => int $year) { ``` or: ``` $foos = [new Foo("one"), new Foo("two")]; foreach ($foos as Foo $foo) { ``` Now I remember this has already been requested (e.g. https://externals.io/message/104485#104488), but that's not really the same use case, so I don't know if it could/should be included... Regards, -- Guilliam Xavier -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php