Heterogeneous type parameter variadics and function literals?
Dylan Knutson
tcdknutson at gmail.com
Fri May 17 00:13:47 PDT 2013
Hello, Please correct me if I'm not using a term right; I'm new
to the language, so I'm not all that familiar with what term
applies to what :-)
I'm in kind of a bind here: I've got to, at compile time, do some
conditional logic within a function literal based on the number
of arguments passed. I could get away with a homogeneous variadic
a-la "function void(auto args...)", however, if no arguments are
passed, then the compiler infers args to be of type void[], which
is a no-go for the compiler.
Furthermore, in my case, I can't declare the type of arguments
that the literal takes, as I don't know what types of parameters
they want to call the literal with ahead of time.
This leaves me with a single option (I think, there's probably
some other way I overlooked): Use a heterogeneous variadic(?).
Type parameters happen to be not valid on function literals as
far as I can tell, so that's a no-go. I'm a little confused by
this actually, as I can't really see why type parameters on
function literals would be a "bad thing", and it seems like the
solution that'd make most sense for D to allow.
So, is there any way to do something like this incorrect code:
auto opt_args = function string(Ctx...)(Ctx args) {
static if(args.length > 1) {
//Do something with args
return "Foo";
} else {
writeln "Bar";
}
}
opt_args(); // "Bar"
opt_args(1); // "Foo"
except in a way the compiler won't throw up at? It'd be *very*
useful for metaprogramming.
Thank you,
Dylan
More information about the Digitalmars-d-announce
mailing list