Hi Tyson,

Regardless of what others are saying, I personally like your RFC. The
reasons are following:
1. Because named keys are valid PHP names, they are resolved at the
parsing level. Thus, they will not be validated again and again.
Later, no constants are looked for.
2. At the runtime level, no variable location is done.
3. Since all keys are to be passed as PHP names, both single quoted
and double quoted strings are not dealt with. Similarly, concatenation
operations are not also performed.
4. Because all named keys are strings by default, type checking is not enforced.
5. `key=>value` costs 2 characters without spaces. If a keyboard user,
like me, wants to make its code easier to navigate, it adds two spaces
to either sides of `=>` operator. Then, single quoted and double
quoted strings have two extra characters, too! Quotes + spaces +
assignment =:
2 + 2 + 2 = 6. If your suggested feature is added, 6 will be reduced
to two — a colon follows a name, and a space follows the colon. Yay,
we have formulated a solution that simplifies keyboard navigation.

Yes, the idea of making anonymous classes/objects easier seams also
compelling. Since this is a discussion, I propose to use braces `{}`
instead of brackets `[]` and follow the pattern that Tyson is
suggesting. I know that it is similar to JavaScript, but it will be
different in one regard that it disallows the use of strings
(regardless of whether they are literals).

Because it requires extra effort to perform array-related operations
on objects, I prefer these two syntaxes:
1. `array(a: 1, b: 2, c: 3);`
2. `[a: 1, b: 2, c: 3];`

Best Regard
Hamza Ahmad


On 6/22/21, tyson andre <tysonandre...@hotmail.com> wrote:
> 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
>
>

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

Reply via email to