Trying to reproduce Node.js async waterfall pattern.. (Meta-programming)

Philippe Sigaud via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jun 23 13:20:56 PDT 2014


On Mon, Jun 23, 2014 at 9:39 PM, Christian Beaumont via
Digitalmars-d-learn <digitalmars-d-learn at puremagic.com> wrote:

> Each function is given a callback, that when called, steps the waterfall
> forward on to the next function to process.  If an error is passed to the
> callback (instead of null), then the waterfall stops processing and calls
> the final callback at the end of the chain.

Just to be sure: whether or not an error is passed to a callback, the
final callback is always called?
I mean, the last callback could also be called *only when something
fails*, a bit like a default case in a switch, or an error-handling
routine.


> 1) I can't see any way to get the compiler to deduce the type of "Funcs...".

What do you mean? The compiler does deduce the type of Funcs.

> I had an urge to somehow specialize the variadic "Funcs..." but I couldn't
> figure out any syntax to do that.

What do you mean by 'specialize'?

>  Well, because of that, I have to
> repeatedly say (Callback cb) instead of (cb).

(cb) { cb(null, "one");} is possible, but that means it's a function
template, not a function.
You can get this syntax by making the callbacks template arguments,
which means they must be known at compile-time. Is that OK with you or
do you need the possibility to define the callbacks at runtime?



> 2) I can't see a path to flow the output type of the previous callback to
> the input "TLastResult" for the next... so it's stuck on "string" :(

I don't get it: none of your callbacks have a return type per se: they
all return 'void'.
Do you want callbacks that really return something?


> 3) I had to use a specialization to deal with the "head" case; (the first
> function doesn't have an input from a previous result). Minor, but niggly.

You can test if func[0] takes one or two arguments:

import std.traits: isCallable, ReturnType;

static if (isCallable!(Func[0]) && ReturnType!(Func[0]).length == 1)


More information about the Digitalmars-d-learn mailing list