On Tuesday, 22 December 2020 at 21:11:12 UTC, Andre Pany wrote:
I am really confused, why is this valid:
void sample(string[string] s = string[string].init){}

while this causes syntax errors?

void sample_invalid1(double[string] s = double[string].init){}
void sample_invalid2(int[int] s = int[int].init){}

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

Reply via email to