Bug 11936 – Allow non-`ref` parameters in `foreach` over range `delegate`/`opApply`

Status
NEW
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-01-16T00:05:21Z
Last change time
2024-12-13T18:16:09Z
Assigned to
No Owner
Creator
Denis Shelomovskii
Moved to GitHub: dmd#18761 →

Comments

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
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/18761 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB