www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why is 'scope' so weak?

reply "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> writes:
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
Nov 23 2010
next sibling parent reply Lutger Blijdestijn <lutger.blijdestijn gmail.com> writes:
Lars T. Kyllingstad wrote:

 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;
     }
Most likely it is not yet implemented? It's hard to find something on this topic, I couldn't find anything in the spec or tdpl. I did found this one post by Andrei about your question: http://permalink.gmane.org/gmane.comp.lang.d.concurrency/617
 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
Nov 23 2010
parent "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> writes:
On Tue, 23 Nov 2010 13:46:19 +0100, Lutger Blijdestijn wrote:

 Lars T. Kyllingstad wrote:
 
 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;
     }
Most likely it is not yet implemented? It's hard to find something on this topic, I couldn't find anything in the spec or tdpl. I did found this one post by Andrei about your question: http://permalink.gmane.org/gmane.comp.lang.d.concurrency/617
Thanks. He only says that "...too many qualifiers make the language quite baroque." Hopefully there is a better reason than that. ;) -Lars
Nov 23 2010
prev sibling parent "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> writes:
On Tue, 23 Nov 2010 10:17:08 +0000, Lars T. Kyllingstad wrote:

 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.  [...]
I just realised I posted this to the wrong group. I'll repost to digitalmars.D, so please post answers there as well. -Lars
Nov 23 2010