Comment #0 by verylonglogin.reg — 2014-01-16T00:05:21Z
Test code:
---
void main()
{
int delegate(int delegate(int)) dg1; // currently unforeachable
int delegate(int delegate(ref int)) dg2;
static assert( __traits(compiles, { foreach( a; dg1) { } })); // fails
static assert(!__traits(compiles, { foreach(ref a; dg1) { } }));
static assert( __traits(compiles, { foreach(ref a; dg2) { } }));
}
---
As some parameters may represent e.g. temporary values it's useful to show it disallowing `ref` on them.
Comment #1 by nick — 2023-10-06T12:41:16Z
Still fails:
foreach (a; dg1) {}
foreachdelegate.d(6): Error: delegate `dg1(int delegate(int))` is not callable using argument types `(int delegate(ref int __applyArg0) pure nothrow @nogc @safe)`
foreachdelegate.d(6): cannot pass argument `__foreachbody3` of type `int delegate(ref int __applyArg0) pure nothrow @nogc @safe` to parameter `int delegate(int)`
However, this works:
int f(int delegate(int));
foreach (a; &f) {}
Comment #2 by robert.schadek — 2024-12-13T18:16:09Z