Full closures for D

Jari-Matti Mäkelä jmjmak at utu.fi.invalid
Mon Nov 5 16:13:24 PST 2007


BCS wrote:

> string mixins are fun:
> 
> typeof(mixin(expr)) delegate() future(char[] expr)()
> {
>     typeof(mixin(expr)) res;
>     auto t = new Thread({res = mixin(expr);});
>     t.start;
>     return { t.wait; return res; }
> }
> 
> auto hard_result = future!("hard_expression");
> // Do stuff ...
> use_result(hard_result());
> 
> 
> NOTE: this runs into the issue that there is no way to get a template to
> play with the local variables of a function. If there were a way to do
> this I can think of a number of very cool things that could be done.

Yea, bummer :| I would have expected this to work: 

char[] bar(char[] foo) {
  char[] tmp;

  tmp ~= "alias ";
  foreach(p; foo)
    if (p == ',') tmp ~= ", alias ";
    else tmp ~= p;

  return tmp;
}

template future(char[] aliases, char[] expr) {
mixin(typeof(mixin(expr)).stringof ~ ` delegate() future(` ~ bar(aliases) ~
`)()
{
  ` ~ typeof(mixin(expr)).stringof ~ ` res;
  auto t = new Thread({res = mixin(expr);});
  t.start;
  return { t.wait; return res; };
}`);
}

void main() {
  int a = 1, b = 2;
  auto hard_result = future!("a,b", "a+b+39")!()();
}


Seems like Walter has at least two bugs to solve before that happens..



More information about the Digitalmars-d mailing list