[Issue 16551] Compile-time delegate parameters should allow "scope"

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sat May 13 08:00:15 PDT 2017


https://issues.dlang.org/show_bug.cgi?id=16551

Walter Bright <bugzilla at digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla at digitalmars.com
           Hardware|x86_64                      |All
                 OS|Linux                       |All

--- Comment #1 from Walter Bright <bugzilla at digitalmars.com> ---
(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.

--


More information about the Digitalmars-d-bugs mailing list