Re: Typed array properties V2
| From: | John Bafford | Date: | Mon, 20 Jan 2020 01:42:58 +0000 |
| Subject: | Re: Typed array properties V2 | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-108201@lists.php.net to get a copy of this message | ||
> On Jan 19, 2020, at 19:53, Mike Schinkel <mike@newclarity.net> wrote:
>
> P.S. There was also the mention by Levi Morrison that the type[] syntax was a poor one because
> of ambiguity between (?int)[] or ?(int[]). I would argue that the latter would likely occur orders
> of magnitude more often than the former, so I would argue that ?int[] should interpret as ?(int[]),
> and if they want (?int)[] then the developer should use parentheses.
As a thought, perhaps the syntax '[Type]' for an array of Type. That way, you could write
?[int], or [?int], or even ?[?int] and there would be no ambiguity, and no need for parentheses
since the array brackets would serve that purpose.
If we also wanted to allow typing array keys, this syntax could be extended to [string : Type] and
[int: Type], and it would continue to remain unambiguous, even with nested arrays, and with using a
more similar syntax than the docblock syntax array<string, Type>. (It might also be reasonable
to support both variants as aliases of each other.)
Both of these are the syntax Swift uses for arrays and dictionaries, so the syntax has precedence
from another language. Swift also supports both syntaxes as described above ([KeyType : ValueType]
is exactly the same as Dictionary<KeyType, ValueType>), but the shorter bracket syntax is
preferred for readability.
-John