www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - Heterogeneous type parameter variadics and function literals?

reply "Dylan Knutson" <tcdknutson gmail.com> writes:
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
May 17 2013
next sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Friday, May 17, 2013 09:13:47 Dylan Knutson wrote:
 Thank you,
 Dylan
Please post questions like this in D.Learn, not D.announce - Jonathan M Davis
May 17 2013
parent "Dylan Knutson" <tcdknutson gmail.com> writes:
On Friday, 17 May 2013 at 07:21:22 UTC, Jonathan M Davis wrote:
 Please post questions like this in D.Learn, not D.announce

 - Jonathan M Davis
Apologies; I didn't notice where I posted this.
May 17 2013
prev sibling parent "deadalnix" <deadalnix gmail.com> writes:
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.
May 17 2013