digitalmars.D.learn - Why is 'scope' so weak?
- Lars T. Kyllingstad (26/26) Nov 23 2010 If I've understood things correctly, by marking a delegate parameter wit...
- Lutger Blijdestijn (6/41) Nov 23 2010 Most likely it is not yet implemented? It's hard to find something on th...
- Lars T. Kyllingstad (4/37) Nov 23 2010 Thanks. He only says that "...too many qualifiers make the language
- Lars T. Kyllingstad (4/7) Nov 23 2010 I just realised I posted this to the wrong group. I'll repost to
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
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/617Here'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
On Tue, 23 Nov 2010 13:46:19 +0100, Lutger Blijdestijn wrote:Lars T. Kyllingstad wrote:Thanks. He only says that "...too many qualifiers make the language quite baroque." Hopefully there is a better reason than that. ;) -LarsIf 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
Nov 23 2010
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