Variable arguments with file and line information?

Namespace rswhite4 at googlemail.com
Sat Nov 16 15:09:53 PST 2013


On Saturday, 16 November 2013 at 22:57:35 UTC, Namespace wrote:
> Hi.
> Is it possible to write something like that?
> ----
> void error(Args...)(string msg, Args args, string file = 
> __FILE__, size_t line = __LINE__) { ... }
> ----
> ?
> Currently not, but how could it be done? I wont like to write:
> ----
> error(format(msg, args));
> ----
>
> Thanks in advance. :)

It is always surprising how quickly one has found its own 
solution, after you have posted here... :)

----
import std.stdio;
import std.string : format;

template error(string file = __FILE__, size_t line = __LINE__, 
Args...) {
	void error(string msg, Args args) {
		static if (args.length != 0)
			msg = .format(msg, args);
		
		writeln(.format(msg ~ ". In file %s on line %d.", file, line));
	}
}

void main()
{
	error("hallo");
	error("Hallo %s.", "du da");
}
----


More information about the Digitalmars-d-learn mailing list