Hi Mel Dafert,

> >I would prefer an improved syntax for creation of anonymous objects. This
> >is something which I have been annoyed with, myself.I'd like to see a
> >simple way of creating anonymous objects with typed properties.
> 
> Another advantage arrays currently have over anonymous objects is 
> destructuring -
> if this was (somehow?) also made possible with objects, this would be the
> best of both worlds.
> (Returning multiple named values from a function is also mentioned in the
> use-cases of the RFC.)
> I know this works:
>
> ```
> [ "foo" => $foo, "baz" => $baz ] = (array) $object;
> ```
> 
> (Alternatively also using get_object_vars() instead of casting.)
> But we both have to convert to an intermediate array again, and lose the
> type information of the object (eg. for static analysis), so this could also 
> be
> made more ergonomic if we want to go down the anonymous object route.

Ideas for syntax:

There's `object(foo: $foo, 'hyphenated-key' => $key) = $object;`,
but that would require making `object` a reserved keyword, and it currently 
isn't one.

- I had suggested using `$x = object(foo: $foo);` as a shorthand for `$x = 
(object)['foo' => $foo];`
  at some point. Feedback was negative for largely unrelated reasons (stdClass 
in general). https://externals.io/message/112082

`object{foo: $foo}` seems similarly unambiguous by requiring at least one 
property but would also require a new reserved word

Or `({ foo: $foo }) = $object;` - On second thought, I'm against that - that is 
very likely to conflict with possible future proposals to parse block 
expressions.
(e.g. getting parsed as a label for a goto followed by returning the variable 
$foo.)

`list{ foo: $foo } = $object` is unambiguous, I guess

`(object)['foo' => $foo] = $object;` was another idea but that wouldn't even 
work.
It's already valid syntax that is parsed as casting an assignment to an object.

`->{'foo' => $foo} = $object;` may be parseable but doesn't make much sense.

Thanks,
Tyson

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to