Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
December 22, 2020 Why is (int[int] s = int[int].init) not allowed | ||||
---|---|---|---|---|
| ||||
Hi, 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){} Kind regards André |
December 22, 2020 Re: Why is (int[int] s = int[int].init) not allowed | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andre Pany | 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
|
December 22, 2020 Re: Why is (int[int] s = int[int].init) not allowed | ||||
---|---|---|---|---|
| ||||
Posted in reply to ag0aep6g | 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
|
December 22, 2020 Re: Why is (int[int] s = int[int].init) not allowed | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andre Pany Attachments:
| On Tue, Dec 22, 2020 at 10:15 PM Andre Pany via Digitalmars-d-learn < digitalmars-d-learn@puremagic.com> wrote: > Hi, > > 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){} > > Kind regards > André > As has been said this is an oddity in the grammar. But why would anyone need to use this anyway? void sample_invalid2(int[int] s = int[int].init){} seems really awful to me anyway. |
December 22, 2020 Re: Why is (int[int] s = int[int].init) not allowed | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozak | On 12/22/20 5:44 PM, Daniel Kozak wrote:
> On Tue, Dec 22, 2020 at 10:15 PM Andre Pany via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com <mailto:digitalmars-d-learn@puremagic.com>> wrote:
>
> Hi,
>
> 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){}
>
> Kind regards
> André
>
>
> As has been said this is an oddity in the grammar. But why would anyone need to use this anyway?
>
> void sample_invalid2(int[int] s = int[int].init){}
>
> seems really awful to me anyway.
Yeah:
void sample_valid(int[int] s = null)
-Steve
|
December 23, 2020 Re: Why is (int[int] s = int[int].init) not allowed | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer Attachments:
| Dne st 23. 12. 2020 1:00 uživatel Steven Schveighoffer via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> napsal: > On 12/22/20 5:44 PM, Daniel Kozak wrote: > > On Tue, Dec 22, 2020 at 10:15 PM Andre Pany via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com <mailto:digitalmars-d-learn@puremagic.com>> wrote: > > > > Hi, > > > > 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){} > > > > Kind regards > > André > > > > > > As has been said this is an oddity in the grammar. But why would anyone need to use this anyway? > > > > void sample_invalid2(int[int] s = int[int].init){} > > > > seems really awful to me anyway. > > Yeah: > > void sample_valid(int[int] s = null) > > -Steve Yes AA.init is null per doc. https://dlang.org/spec/hash-map.html#construction_and_ref_semantic |
December 23, 2020 Re: Why is (int[int] s = int[int].init) not allowed | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozak | On Wednesday, 23 December 2020 at 07:08:31 UTC, Daniel Kozak wrote:
> Dne st 23. 12. 2020 1:00 uživatel Steven Schveighoffer via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> napsal:
>
>> On 12/22/20 5:44 PM, Daniel Kozak wrote:
>> > [...]
>>
>> Yeah:
>>
>> void sample_valid(int[int] s = null)
>>
>> -Steve
>
>
> Yes AA.init is null per doc.
>
> https://dlang.org/spec/hash-map.html#construction_and_ref_semantic
Thanks for clarification, I was not aware that AA.init is technically the same as null.
Kind regards
Andre
|
Copyright © 1999-2021 by the D Language Foundation