Syntax ideas for `ref` returning delegate and FP parameters

Q. Schroll qs.il.paperinik at gmail.com
Sat Apr 10 14:27:31 UTC 2021


Currently in D, one cannot directly express that a function 
parameter's type is a delegate or function pointer that returns 
by `ref`. If you write
```D
void f(ref int delegate() dg) { .. }
```
then `dg` will be bound by reference which is probably not what 
the user likely intended. It is equivalent to:
```D
alias DG = int delegate();
void f(ref DG dg) { .. }
```
I guess it should be possible to express
```D
alias refDG = ref int delegate();
void f(refDG dg) { .. }
```
without an alias. The obvious idea is allowing
```D
void f(int delegate() ref dg)
```
but I really dislike not having `ref` next to the return type. I 
guess most users read `ref int` as a pseudo type, so having them 
separated is misleading.

Another was a breaking change making `ref` part of the delegate 
type when spelled out:
```D
void f(ref int delegate() dg) { .. }
```
would take `dg` by copy and `typeof(dg)` would be `ref int 
delegate()`. If you want to take the delegate by reference, use 
an alias.

A third one that is a little obscure is allowing `ref(int)` as a 
syntax for delegate and function pointer return types. Then it 
would be akin to `const int* method()` being different from 
`const(int*) method()`.

Do you guys have any better ideas? I don't really like mine.


More information about the Digitalmars-d mailing list