D Primary Type Syntax is Essentially Implemented!

Nick Treleaven nick at geany.org
Thu Dec 21 15:34:11 UTC 2023


On Tuesday, 23 May 2023 at 22:18:35 UTC, Quirin Schroll wrote:
> #### Parameters
> ```d
> // `a` takes its parameter by value;     the parameter returns 
> by reference.
> // `b` takes its parameter by reference; the parameter returns 
> by value.
> void a( ref (int function()) ) { }
> void b((ref  int function()) ) { }
> // Note: The parameter storage class has priority
> //       over the value category of the parameter’s return type.
>
> // `c` is `a` without clarifiying parentheses.
> void c( ref  int function()  ) { }
>
> static assert(!is( typeof(&a) == typeof(&b) ));
> static assert( is( typeof(&a) == typeof(&c) ));
> ```
> #### Return types
> ```d
> // `x` returns by reference; the returned function ptr returns 
> an `int` by value.
> // `y` returns by vale;      the returned function ptr returns 
> an `int` by reference.
>  ref (int function()) x() { static typeof(return) fp = null; 
> return fp; }
> (ref  int function()) y() => null;
> // Note: The value category of the declared function has 
> priority
> //       over the value category of the return type.
>
> // `z` is `x` without clarifiying parentheses.
>  ref  int function()  z() => x();
>
> static assert(!is( typeof(&x) == typeof(&y) ));
> static assert( is( typeof(&x) == typeof(&z) ));
> ```

The solution seems to be more than what is required to solve the 
`ref` problem. It also means we introduce multiple ways of 
spelling the same type. And as you found, it can break code:
https://forum.dlang.org/post/eguzeiayudwqcvaencdf@forum.dlang.org

Instead, why not implement just the subset of this proposal that 
is actually needed - add this rule:

```md
BasicType:
     `(` `ref` Type `)`
```
This rule doesn't allow `a` or `x`, yet allows `b` and `y`, and 
works for variable declarations. It doesn't conflict with any 
expression AFAIK.

And if the full proposal is wanted later, the subset can be 
extended without breakage.


More information about the Digitalmars-d mailing list