Can't use variadic arguments to functions that use templates

H. S. Teoh hsteoh at quickfur.ath.cx
Tue Jul 23 12:12:51 PDT 2013


On Tue, Jul 23, 2013 at 08:54:12PM +0200, Jesse Phillips wrote:
> On Tuesday, 23 July 2013 at 16:22:38 UTC, JS wrote:
> >On Tuesday, 23 July 2013 at 16:15:03 UTC, Jesse Phillips wrote:
> >>On Tuesday, 23 July 2013 at 14:03:01 UTC, JS wrote:
> >>>I don't think you understand(or I've already got confused)...
> >>>
> >>>I'm trying to use B has a mixin(I don't think I made this
> >>>clear). I can't use it as a normal function. e.g., I can't
> >>>seem to do mixin(B(t)). If I could, this would definitely
> >>>solve my problem.
> >>...[Code]...
> >What good does that do?
> >
> >What if I want to use a run-time variable in the mix?
> 
> Ah, I understand now you're interested in:
> 
> string concat(alias Data...)() { ... }
> 
> This does not exist, but maybe it should... I don't know of a
> workaround to get this behavior.

Is this by any chance related to that other thread about compile-time
optimized join()? 'cos if it is, I've already solved the problem via
templates:

	import std.stdio;
	
	template tuple(args...) {
		alias tuple = args;
	}
	
	/**
	 * Given a tuple of strings, returns a tuple in which all adjacent compile-time
	 * readable strings are concatenated.
	 */
	template tupleReduce(args...)
	{
		static if (args.length > 1)
		{
			static if (is(typeof(args[0])==string) &&
				__traits(compiles, { enum x = args[0]; }))
			{
				static if (is(typeof(args[1])==string) &&
					__traits(compiles, { enum x = args[1]; }))
				{
					alias tupleReduce = tupleReduce!(args[0] ~
								args[1], args[2..$]);
				}
				else
				{
					alias tupleReduce = tuple!(args[0], args[1],
								tupleReduce!(args[2..$]));
				}
			}
			else
			{
				alias tupleReduce = tuple!(args[0],
							tupleReduce!(args[1..$]));
			}
		}
		else
		{
			alias tupleReduce = args;
		}
	}
	
	void main() {
		string x = "runtime1";
		string y = "runtime2";
		auto arr = [ tupleReduce!("a", "b", x, "c", "d", y, "e", "f", "g", x) ];
		writeln(arr);
	}

The output is:

	["ab", "runtime1", "cd", "runtime2", "efg", "runtime1"]

All compile-time readable strings in the list have been concatenated at
compile-time.


T

-- 
I don't trust computers, I've spent too long programming to think that
they can get anything right. -- James Miller


More information about the Digitalmars-d-learn mailing list