Variadic Templates or functions with Lazy Evalutaion
Chris Nicholson-Sauls
ibisbasenji at gmail.com
Tue Aug 14 16:47:55 PDT 2007
convert wrote:
> I am trying to write a log function that can take a variable number of arguments and does lazy evaluation on each of them.
>
> -------------
> void log(A...) (int level ,lazy A args)
> {
> if(level > 1) Stdout(args).newline();
> }
>
> log(2, "Hello");
>
> -----------
>
> Produces:
>
> test2.d(22): template instance test2.log!(char[6u]) error instantiating
> test2.d(22): Error: functions cannot return static array char[6u]
In this case, the problem is that string literals are always static/fixed-length arrays,
which cannot be returned from functions -- and lazy parameters are "on the fly" wrapped as
delegate functions. Ie, the parameter 'lazy int' is typed the same as 'int delegate()'.
It will work as written if you pass a full-slice of the literal -- or if the value is in a
dynamic/variable-length array to begin with.
log(2, "Hello"[]);
This isn't so bad unless you intend to have several literal strings in a call to log...
then it gets a little awkward looking.
log(2, "Hello"[], "from"[], "D"[]);
> And
>
> void log(int level ,lazy ...)
>
> produces:
>
> test2.d(15): basic type expected, not ...
>
> Should any of these work?
>
This will not work because of the way (as I mentioned earlier) lazy parameters are
re-written as delegates. A function must have a formal return type.
-- Chris Nicholson-Sauls
More information about the Digitalmars-d-learn
mailing list