static foreach and inline if

JS js.mdnq at gmail.com
Sat Jul 27 10:14:34 PDT 2013


I'd like to have foreach and inline if in templates:


The tIf doesn't work properly but I'm not sure why


template tuple(args...) { alias tuple = args; }

template tIf(bool cond) { alias tIf = tuple!(); }
template tIf(bool cond, alias T)
{
	static if (cond) alias tIF = T; else alias tIF = tuple!();
}

template tForEach(alias func, args...)
{
	pragma(msg, args);
	static if (args.length == 0)
		alias tForEach = tuple!();
	else
	{
		static assert(is(typeof(func(args[0]))), "tForEach: function 
not valid for argument : "~typeof(args[0]).stringof);
		pragma(msg, "1"~typeof(args[0]).stringof);
		alias tForEach = tuple!(func(args[0]), tIf!(args.length > 1, 
tForEach!(func, args[1..$])));
	}
}


e.g.,

writeln(tForEach((string x) => x ~ ";", "a", "b"));

should print a;b;



More information about the Digitalmars-d-learn mailing list