Grammar question. TypeSuffix - what is [ AssignExpression .. AssignExpression ] production for?

WB witold.baryluk at gmail.com
Mon Sep 2 00:16:14 UTC 2024


I am reading D grammar again

and spotted on https://dlang.org/spec/grammar.html#TypeSuffix and 
https://dlang.org/spec/type.html#grammar

Something weird

```
TypeSuffix:
     *
     [ ]
     [ AssignExpression ]
     [ AssignExpression .. AssignExpression ]
     [ Type ]
     delegate Parameters MemberFunctionAttributesopt
     function Parameters FunctionAttributesopt
```

1st - pointers, 2nd - dynamic arrays, 3rd - static arrays, 5th - 
associative arrays, 6/7 - delegate types.


The 4th production would mean this is legal (syntactically) 
variable definition:

```d
string[5 .. 9] x;
```


My random guess is that this for creating some kind of range 
checked arrays with non-zero start? I believe languages like 
Pascal and Ada had similar constructs.


Of course that does not work for any built in type. Nor I see how 
this could work for user defined type (maybe a static `opSlice` 
on a user type that creates a proxy object, but that I fails to 
see how exactly).



Any idea what is this `[ AssignExpression .. AssignExpression ]` 
production for actually?


Is it for some tuple / type sequence slicing? (I guess it is 
called AliasSeq in current Phobos)

However, in places like 
https://dlang.org/library/std/meta/alias_seq.html  I cannot see 
this being mentioned (second example kind of hints at it tho).

I also looked at 
https://dlang.org/spec/function.html#d_style_variadic_functions  
for D-style variadic functions, but I think it is not that.

So maybe https://dlang.org/spec/template.html#variadic-templates 
, where more specifically 
https://dlang.org/spec/template.html#seq-ops  mentions slicing 
operation on these sequences ?


Could we maybe improve a grammar a bit to make it a bit cleaner, 
by naming productions?

Something like this:

```
TypeSuffix:
     Pointer
     Array
     TypeSequenceSlice
     FunctionPointer
     Delegate
```

and move existing productions to these sub-rule there accordingly.

Or similar.



More information about the Digitalmars-d mailing list