Third and Hopefully Last Draft: Primary Type Syntax

Tim tim.dlang at t-online.de
Sun Sep 22 10:58:55 UTC 2024


On Saturday, 21 September 2024 at 01:01:22 UTC, Quirin Schroll 
wrote:
> The obligatory 
> [permalink](https://github.com/Bolpat/DIPs/blob/0562d0c1708f4f8bb79e72392218154ee39b1d4f/DIPs/DIP-2NNN-QFS.md) and [latest draft](https://github.com/Bolpat/DIPs/blob/PrimaryTypeSyntax/DIPs/DIP-2NNN-QFS.md)

The grammar changes look good. I found some new ambiguities, but 
the implementation seems to always prefer the old meaning, so it 
should be no problem.

### Attributes with optional parens

```d
// deprecated (size_t) x1 = 1; // Syntax error
// align (size_t) x2 = 1; // Syntax error
// package (size_t) x3 = 1; // Syntax error
// extern (size_t) x4 = 1; // Syntax error
struct UDA{}
// @UDA (size_t) x5 = 1; // Syntax error
```

The attributes `deprecated`, `align`, `package` and `extern` as 
well as
UDAs can be followed with optional arguments in parens, like the
deprecation message. These parens are now ambiguous with a basic 
type in
parens.

The implementation seems to always try to parse the parens as 
arguments
for the attribute, so it remains backward compatible.

Maybe this could be confusing for the user, when a declaration 
uses a
type in parens and later an attribute is added.


### Scope guards

```d
alias exit = Object;
Object x1;
void main()
{
     scope (exit) x1 = new Object(); // Still a scope guard
     // scope (Object) x2 = new Object(); // Syntax error
     // scope (int) x3 = 3; // Syntax error
     @0 scope (exit) x4 = new Object(); // Declares variable with 
type exit
}
```

The first statement is a scope guard with the current grammar. 
With the
new grammar it could also be a variable declaration of type 
`exit` and
storage class `scope`. The implementation still parses it as a 
scope
guard, so it remains backward compatible.

The next line could also be a variable declaration, but it is 
still
parsed as a scope guard. DMD then prints an error, because 
`Object`
is not a valid scope identifier. The line with `x3` is a syntax 
error
for the same reason.

The last statement is parsed as a variable declaration, because 
scope
guards can't have UDAs.

### Function literals

```d
auto test1 = function (float){return 0;};
// auto test2 = function (float)(int){return 0;}; // Syntax error
```

Function literals have an optional return type and optional 
parameters.
The type `float` for `test1` could be a parameter or a return 
type in
parens. The implementation always parses the parens as parameters,
so it remains backward compatible.

The second function literal has both a return type and 
parameters, but
it results in a syntax error, because the parens are parsed as
parameters and no other parens are expected after that.

### Anonymous classes

```d
void main()
{
     auto o1 = new class (Object) {};
}
```

The parens could be constructor arguments or a basic type in
`AnonBaseClassList?`. The implementation always tries to parse
constructor arguments, which should be fine.



More information about the dip.development mailing list