seeding the pot for 2.0 features

BCS BCS at pathlink.com
Thu Jan 25 10:08:29 PST 2007


Kristian Kilpi wrote:
> On Wed, 24 Jan 2007 02:09:09 +0200, BCS <ao at pathlink.com> wrote:
> [snip]
> 
>> my choice: dynamic init structs and explicit context for delegates
>>
>> int i,j,k;
>> // pickle i,j,k then use them
>> auto (new struct {int i_ = i; int j_ = j; int k_ = k; }).delegate(int  
>> m){return ((i_ * m)+j_)*m +k_;}
>>
> 
> While we are at it, why not just let the compiler to generate context 
> for  delegates? ;)
> E.g.
> 
>   int i, j;
>   return int delegete(int m) {return i * m + j;}
> ->
>   return int (new struct {int i_ = i; int j_ = j}).delegete(int m) 
> {return  i_ * m + j_;}


I think that has been proposed but has some problems. For instance


auto dg = int delegate(int m) {return i * m + j;}

fnUsingDg(dg);	// doesn't need context

i++;
j++;	// did that effect the context?

if(boolFn())
	return dg;	// now we need context
else
			// or do we??
	return int delegate(int m) {return i + j;}

the explicit form has some advantages in clarity

maybe a cut down version


auto {i; j; k;}.dup.delegate(int m)
	{
		return ((i_ * m)+j_)*m +k_;
	}

going back to my other idea (the one involving implementing an interface 
from its tuple) you might be able to build a template that builds and 
instance a struct


BuildContext(alias A...){ ... }
int i,j,k;

return BuildContext!(i,j,k).delegate()
   {
     return i+j+k;
     // refers to members of context not function
   };

I'm not quite sure how to make it work under the hood but it would 
involve having aliases tuples that can act as:
   a type list to build structs
   a name list to name struct members
   a type/argument list to form a function
   a symbol list to access the symbols instancing the templates

BuildContext!(alias A...)()
{
   auto ret = new struct
   {
     // same types, same symbols
     A.types A.symbols;

     void set(A.types val)
     {
       // copy args
       *this = val;
     }
   }
   // call using whatever was used to instance the template
   ret.set(A.values);

   return ret;
}

Ahh, Errr. That would be a trick, and is asking for quite a lot.



More information about the Digitalmars-d mailing list