www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Feature Request: Support delegate literal context skipping

reply downs <default_357-line yahoo.de> writes:
In the following situation

class Foo {
  int e;
  void delegate() bar() {
    return { e++; };
  }
}

the delegate literal will not work. This is because its context is bar,
and bar has ended. But the literal doesn't *need* anything from bar.

Because of this, I propose in situations where a delegate literal
doesn't access the immediate surrounding stack frame, its context be the
first frame (or class) that it does access.
So in this example, { e++; } 's .ptr would point at the respective
instance of Foo, allowing this to work.

Disadvantages: None
Advantages: potentially highly helpful with user interface code, where
such callbacks are commonplace.

  Looking forward to comments
 --downs
Oct 12 2007
next sibling parent Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
downs wrote:
 In the following situation
 
 class Foo {
   int e;
   void delegate() bar() {
     return { e++; };
   }
 }
 
 the delegate literal will not work. This is because its context is bar,
 and bar has ended. But the literal doesn't *need* anything from bar.
 
 Because of this, I propose in situations where a delegate literal
 doesn't access the immediate surrounding stack frame, its context be the
 first frame (or class) that it does access.
 So in this example, { e++; } 's .ptr would point at the respective
 instance of Foo, allowing this to work.
IIRC this has been proposed before. vote++: Or should I say "return { vote++; }"? ;)
Oct 12 2007
prev sibling next sibling parent Tom S <h3r3tic remove.mat.uni.torun.pl> writes:
{	downs wrote:
	> In the following situation
	>
	> class Foo {
	>   int e;
	>   void delegate() bar() {
	>     return { e++; };
	>   }
	> }
	>
	> the delegate literal will not work. This is because its context is bar,
	> and bar has ended. But the literal doesn't *need* anything from bar.
	>
	> Because of this, I propose in situations where a delegate literal
	> doesn't access the immediate surrounding stack frame, its context be the
	> first frame (or class) that it does access.
	> So in this example, { e++; } 's .ptr would point at the respective
	> instance of Foo, allowing this to work.
	>
	> Disadvantages: None
	> Advantages: potentially highly helpful with user interface code, where
	> such callbacks are commonplace.
}.votes++;

-- 
Tomasz Stachowiak
http://h3.team0xf.com/
h3/h3r3tic on #D freenode
Oct 12 2007
prev sibling next sibling parent BCS <BCS pathlink.com> writes:
downs wrote:
 In the following situation
 
 class Foo {
   int e;
   void delegate() bar() {
     return { e++; };
   }
 }
 
 the delegate literal will not work. This is because its context is bar,
 and bar has ended. But the literal doesn't *need* anything from bar.
 
 Because of this, I propose in situations where a delegate literal
 doesn't access the immediate surrounding stack frame, its context be the
 first frame (or class) that it does access.
 So in this example, { e++; } 's .ptr would point at the respective
 instance of Foo, allowing this to work.
 
 Disadvantages: None
 Advantages: potentially highly helpful with user interface code, where
 such callbacks are commonplace.
 
   Looking forward to comments
  --downs
The way I want it is that a delegate literal can specify the context like this. class Foo { int e; void delegate() bar() { return this.{ e++; }; //note "this." } }
Oct 12 2007
prev sibling parent Robert Fraser <fraserofthenight gmail.com> writes:
downs Wrote:

 In the following situation
 
 class Foo {
   int e;
   void delegate() bar() {
     return { e++; };
   }
 }
 
 the delegate literal will not work. This is because its context is bar,
 and bar has ended. But the literal doesn't *need* anything from bar.
 
 Because of this, I propose in situations where a delegate literal
 doesn't access the immediate surrounding stack frame, its context be the
 first frame (or class) that it does access.
 So in this example, { e++; } 's .ptr would point at the respective
 instance of Foo, allowing this to work.
 
 Disadvantages: None
 Advantages: potentially highly helpful with user interface code, where
 such callbacks are commonplace.
Disadvantages: That doesn't sound easy to implement. I like the idea a lot, though.
Oct 12 2007