Why is (int[int] s = int[int].init) not allowed
Andre Pany
andre at s-e-a-p.de
Tue Dec 22 22:05:02 UTC 2020
On Tuesday, 22 December 2020 at 22:02:54 UTC, ag0aep6g wrote:
> On Tuesday, 22 December 2020 at 21:11:12 UTC, Andre Pany wrote:
>> [...]
>
> Looks like an oddity in the grammar.
>
> `string` is an alias, meaning it's an identifier. And an
> identifier is a valid expression to the grammar. So
> `string[string]` is parsed as an IndexExpression. Only during
> semantic analysis does the compiler figure out that it's
> actually a type.
>
> `double` and `int` aren't identifiers. They're keywords. And
> they're always types, never expressions. So `int[int]` cannot
> be parsed as an IndexExpression. It's parsed as a Type instead.
> And for a (grammatical) Type, there is no rule that allows
> `Type.Identifier`.
>
> You can work around with parentheses:
>
> (double[string]).init;
> (int[int]).init
Thanks a lot!
Kind regards
Andre
More information about the Digitalmars-d-learn
mailing list