Full closures for D
BCS
BCS at pathlink.com
Mon Nov 5 14:37:25 PST 2007
Craig Black wrote:
> "Mikola Lysenko" <mclysenk at mtu.edu> wrote in message
> news:fgns3v$1b13$1 at digitalmars.com...
>
>>I'm excited. For the first time it is now possible to do futures in 7
>>lines of code:
>>
>>T delegate() future(T)(lazy T expr)
>>{
>> T res;
>> auto t = new Thread({res = expr();});
>> t.start;
>> return { t.wait; return res; }
>>}
>>
>>
>>Which could then be used as follows:
>>
>>auto hard_result = future( hard_expression );
>>
>>// Do stuff ...
>>
>>use_result(hard_result());
>>
>
>
> Very cool stuff! Did you test this code to see if it actually works?
>
> A little off topic, but I was looking at your code and wondering if "lazy"
> always means that the expression becomes a delegate? When a template is
> used, the compiler could embed the expression directly rather than creating
> a delegate. Since D is big on compile-time optimization, I was wondering if
> it does this.
>
> -Craig
>
>
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.
More information about the Digitalmars-d
mailing list