Trouble with anon delegates.

Pragma ericanderton at yahoo.removeme.com
Wed Jan 17 08:55:59 PST 2007


BCS wrote:
> Reply to Pragma,
>>
>> auto rule = ws >> '(' >> +Argument >> ')' >> { writefln("args
>> parsed"); };
>>
>> It works fine for this kind of stuff, but once you graduate beyond
>> there and want to bind terminals to class members, you need the
>> ability to perform filtering and aritmetic on them.  But once you do
>> that, you find that your stack frame is all out of whack. :(
> 
> I tend to use structs for that kind of thing. They entail less overhead 
> than classes.

I would have done the same, but I'm making heavy use of polymorphism to get the operators to work as expected.  However, 
that could be refactored into a struct-oriented design with some clever templates and mixins to emulate inheritance.

> 
> [...]
>> It would be nice if things so succinct would work as I expected.
>> Instead I have to break all the delegates out into proper class
>> methods, which bloats the code and runs contrary to the whole point of
>> having a parser framework like this.
> 
> I've said it befor, What is needed is to allow delegate leterals to 
> expicetly state there context.
> 
> auto c = new C;
> return c.(char[] foo){retrun this.toString() ~ foo;}
> 
> Another thing would be non static init for anon structs. This and a few 
> others would make for some neat code.
> 
> int delegate(int,int,int) Bar(int i, int j, int k)
> {
>   return new struct {
>      int a = i;
>      int b = j;
>      int c = k;
>      }.(int m, int n, int o){ return a*m + b*n + c*o;};
> }

I read the other thread here, in D.learn, on this concept.  I like the ability to anonymously declare any construct, but 
the ability to add literal methods/members to those is where it really shines.  I've been using similar techniques in 
Javascript programming, and it completely transforms the way you approach solving particular problems.  As Walter has 
written a compliant JS engine, you'd think he's be wise to the advantages here?  Maybe there's something else blocking 
this kind of syntax from inclusion into D.

> 
>>
>> The ability to declare *first class* delegates as anonymous and inline
>> like the above is just too tempting to leave alone.  I suppose the
>> only way to have your cake and eat it too is to preserve the stack
>> somehow.
> 
> I guess that is more or less what the above does.
> 
> (Where's the 2.0 wish list?  <g> )
> 

You mean, we dont' have one yet?!


-- 
- EricAnderton at yahoo


More information about the Digitalmars-d-learn mailing list