Bug 21521 – Cannot state ref return for delegates and function pointers
Status
RESOLVED
Resolution
DUPLICATE
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-01-03T02:46:31Z
Last change time
2023-11-08T10:38:56Z
Keywords
spec
Assigned to
No Owner
Creator
Bolpat
Comments
Comment #0 by qs.il.paperinik — 2021-01-03T02:46:31Z
Currently, the grammar does not allow `ref` to occur in the rhs of IsExpression and parameter types. There is no way to express that a function requires its function pointer/delegate parameter to return by reference without using a helper alias:
void f(ref int delegate() dg)
{
static assert(!__traits(isRef, dg)); // fails, dg is ref, not its return
}
void f(int delegate() ref dg) // invalid syntax: found `ref` when expecting
{ }
Same for testing whether DG is a fp or delegate returning by ref:
is(DG : ref int delegate()) // invalid syntax
is(DG : int delegate() ref) // invalid syntax
Workaround:
alias RefFunctionPointer(T, Args...) = ref T function(Args);
alias RefDelegate(T, Args...) = ref T delegate(Args);
The workaround has severe limitations such as attributes and parameter storage classes.
Comment #1 by nick — 2023-11-08T10:38:56Z
*** This issue has been marked as a duplicate of issue 2753 ***