Heterogeneous type parameter variadics and function literals?

deadalnix deadalnix at gmail.com
Fri May 17 02:42:14 PDT 2013


On Friday, 17 May 2013 at 07:13:49 UTC, Dylan Knutson wrote:
> 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.

I don't think so. It should be an empty tuple. args.length will 
be 0 and known at compile time.

> 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.
>

function string(Variant[] args...) {
    // So your stuff.
}

Note you can't use static if. static if is done at compile time, 
so by definition, it is gone at runtime. If you don't know what 
is passed to your function at compile time, then you can't use 
static if. You can't make first class function without specifying 
the compile time parameters, as each set a compile time parameter 
create a new function.


More information about the Digitalmars-d-announce mailing list