Bug 16551 – Compile-time delegate parameters should allow "scope"

Status
RESOLVED
Resolution
LATER
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-09-27T09:27:59Z
Last change time
2021-12-08T19:06:52Z
Assigned to
No Owner
Creator
Eyal

Comments

Comment #0 by eyal — 2016-09-27T09:27:59Z
Currently, in order to pass scoped delegates and avoid GC, one must pass the delegates as run-time parameters, with undesirable consequences. It would be very helpful to allow them to be passed as scoped and in compile-time. alias Foo1 = void delegate() @nogc; alias Foo2 = scope void delegate() @nogc; // <-- this "scope" seems to be completely ignored! // Syntax error, why? :( // void foo(scope Foo1 x)() @nogc; // This is allowed, but Foo2 is not truly "scope" void foo(Foo2 x)() @nogc; void main() @nogc { int x; foo!({x+=1;}); // Claims to allocate with the gc, it does not! }
Comment #1 by bugzilla — 2017-05-13T15:00:15Z
(In reply to Eyal from comment #0) > // Syntax error, why? :( > // void foo(scope Foo1 x)() @nogc; It's because 'scope' is not a type constructor, it is a storage class. > alias Foo2 = scope void delegate() @nogc; // <-- this "scope" seems to be completely ignored! This is because 'scope' here applies to what the delegate does with its 'this' pointer (aka 'context' pointer, 'frame' pointer, 'dynamic link' pointer). foo() could still store the delegate into a global variable, hence allowing it to escape and hence a closure is necessary. Currently, the following compiles successfully: --- alias Foo2 = void delegate() @nogc; // no 'scope' necessary here void foo(scope Foo2 x) @nogc { } void main() @nogc { int x; foo({x+=1;}); } --- Turning on inlining should remove the overhead of making it a runtime parameter. Adding 'scope' as an allowed storage class for template alias parameters would be a significant and complex extension, and would need a strong use case rationale. This would need to have a DIP created for it.