Template context

Daniel Keep daniel.keep+lists at gmail.com
Sun Jan 7 07:46:31 PST 2007


Ok, so I've just given up on finding any reasonable way to make this any 
easier:

 > trace("module.function_name", __FILE__, __LINE__, "fooing the bar");

The problem is, as I'm sure you're aware, that __FILE__ and __LINE__ are 
expanded by the tokeniser, NOT the parser.  Therefore, I propose the 
following:

Templates all gain (either implicitly or via a specially named argument) 
an object called "context", which represents the context within which 
the template was instantiated.  It should have the following 
compile-time properties:

* context.line: line on which the template was instantiated.
* context.file: file in which the template was instantiated.
* context.symbol: the full name of the symbol in which etc.

For example:

 > module foo;
 >
 > void trace(T_Args...)(T_Args args...)
 > {
 >     writef("%s:%d (%s): ", context.file, context.line, context.symbol);
 >     foreach( arg ; args )
 >         writef(arg);
 >     writefln();
 > }
 >
 > void main()
 > {
 >     trace("Hi, ma!");
 > }

Would produce the output:

foo.d:13 (foo.main): Hi, ma!

If any templates want to propogate their context to other templates, 
they can just pass it manually.  Currently, passing this information 
around is painful (and we can't even get the symbol part AFAIK).

Pretty please, Mr. Bright?  *puppy dog eyes*

	-- Daniel

P.S.  We could also (with a *teensy* modification to Phobos) do this:

 > module bar;
 >
 > void raise(T_Exception, T_Args...)(T_Args args)
 > {
 >     auto e = new T_Exception(args);
 >     e.file = context.file;
 >     e.line = context.line;
 >     e.symbol = context.symbol;
 >     throw e;
 > }
 >
 > // blah
 >
 > void main()
 > {
 >     raise!(GasLeakException)("Houston, we have a problem...");
 > }

Error: bar.d line 16 in bar.main: Houston, we have a problem...



More information about the Digitalmars-d mailing list