Futurism lib (futures in D)
Kevin Bealer
kevinbealer at gmail.com
Mon Jan 22 21:59:14 PST 2007
== Quote from Daniel Keep (daniel.keep+lists at gmail.com)'s article
> Kevin Bealer wrote:
...
>
> > 2. You can't pass a char[10] (for example) to a function that uses a char[]
> > input -- instead, you need to ask for the slice, for example, using
> > syntax like:
> >
> > make_future(& work, 1234, "some string"[]);
> > ^^
> >
> > I could probably fix this if I knew how to use static if or "is" to find
> > out whether something is a static array or not. For now, the above syntax
> > is a not-too-painful workaround.
> >
> > Kevin
>
> Of course, the alternative is to move the function out to the template
> argument as an alias, and THEN specialise on the argument type of that
> (which I've done in my OGL/SDL safety templates), which would make it
> look like this:
>
> make_future!(work)(1234, "some string");
>
> At least, I think it would :P
>
> This is one of the reasons I love D: programming in it is like opening
> up a shiny russian doll: every time you think you know what's going on,
> there's a whole 'nother layer to play with.
I didn't fully appreciate this statement until I got the solution working.
Now my head hurt like back when I was learning recursion for the first time,
which is a sign that my mental software is getting a refactoring. ;)
Template metaprogramming requires another layer of meta-thinking. Practice
helps too (with the syntax especially). I think I'm finally starting to
"absorb" what tuples are doing and why variadic stuff works the way it does.
The requirement for "[]" is gone now, but more fundamentally, conversions
between arguments and parameters should happen in the 'right direction'.
This is accomplished by extracting the parameters from the delegate inside
of make_future() (using a helper template). The changes are in SVN but for
those of you just reading along, these are the modified lines:
template FutureTypeGroup(Delegate, Args...) {
alias ReturnType!(Delegate) Return;
alias ParameterTypeTuple!(Delegate) Params;
alias Future!(Return, Params) TFuture;
}
FutureTypeGroup!(Delegate, Args).TFuture
make_future(Delegate, Args...)(Delegate cmd, Args args)
{
return new FutureTypeGroup!(Delegate, Args).TFuture(cmd, args);
}
I think the fact that this change affects so few lines is a testament
to the increasing expressiveness of D.
Kevin
More information about the Digitalmars-d-announce
mailing list