D needs a type expression syntax

Quirin Schroll qs.il.paperinik at gmail.com
Tue May 9 17:15:41 UTC 2023


Given:
 [`Type`](https://dlang.org/spec/type.html#Type) → 
[`BasicType`](https://dlang.org/spec/type.html#BasicType)
 [`BasicType`](https://dlang.org/spec/type.html#BasicType) → 
**`(`**[`Type`](https://dlang.org/spec/type.html#Type)**`)`**


I have found one issue, but it falls under the category of Nobody 
Writes Such Code:
If a type `T` has a `static` indexing operator (`opIndex` or 
similar), then the token sequence `T[]` can denote a 
[`Type`](https://dlang.org/spec/type.html#Type) or an 
[`Expression`](https://dlang.org/spec/expression.html#Expression). If that token sequence is itself in parentheses and not prefixed by a [`TypeCtor`](https://dlang.org/spec/type.html#TypeCtor), then it currently cannot parse as a [`Type`](https://dlang.org/spec/type.html#Type), but with the proposed changes, it could, and in some circumstances, that would make valid working code change meaning.

Example:
```d
enum isType(alias a) = is(a);

struct X
{
     static int opIndex() @safe => 0;
}

// Parsed as a type,  `X[]` is slice of `X`.
// Parsed as a value, `X[]` is `X.opIndex()`.

void main() @safe
{
     static assert(!isType!( 0 ));
     static assert( isType!(int));

     static assert( isType!( X[] ));
     static assert(!isType!((X[]))); // This would fail!
}
```

Again, nobody writes such code and relies on a pair of 
parentheses to make something a value that’s read by any human as 
a type. This is something that is even bad today. Nothing should 
change what it is just because you put it in a pair of 
parentheses.


More information about the Digitalmars-d mailing list