Why is 'scope' so weak?

Lars T. Kyllingstad public at kyllingen.NOSPAMnet
Tue Nov 23 02:17:08 PST 2010


If I've understood things correctly, by marking a delegate parameter with 
'scope' you tell the compiler not to create a true closure for the 
delegate.  Effectively you're saying "I promise not to escape this 
delegate, so you don't need to copy its context to the heap".

In brief, my question is:  Why doesn't the compiler enforce this 
promise?  In particular, why is 'scope' not a type constructor?

(Note that this is mostly a question out of curiosity, and not really a 
proposal for a new feature.  I imagine it has been discussed in the past 
and rejected for some reason.)

Considering that the compiler enforces proper use of pure, nothrow, 
const, and all those other things, it doesn't seem much harder to do the 
same with scope.

As an example, I really can't see a reason why obviously wrong code like 
this should be allowed:

    void delegate() globalDg;

    void foo(scope void delegate() dg)
    {
        globalDg = dg;
    }

Here's a slightly less obvious example, which also compiles successfully:

    void foo(void delegate() dg);  // Who knows what this does?

    void bar(scope void delegate() dg)
    {
        foo(dg);
    }

-Lars


More information about the Digitalmars-d-learn mailing list