Template context

Georg Wrede georg at nospam.org
Sun Jan 7 15:18:35 PST 2007


Daniel Keep wrote:
> 
> 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...

It's a shame that you're such a slow thinker, we could've had this in 
1.0 if you only had come out with it like 3 months ago.

Just kidding, this is an excellent proposition, definitely 1.1 material!
Both of these ideas, actually.

I've always felt that _FILE_ and _LINE_ taste, how should I say, 
preprocessorish or see(plusplus)ish. They're so 1980's.



More information about the Digitalmars-d mailing list