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
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:
alias DG = int delegate();
void f(ref DG dg) { .. }
I guess it should be possible to express
alias refDG = ref int delegate();
void f(refDG dg) { .. }
without an alias. The obvious idea is allowing
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:
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.