Template context

Daniel Keep daniel.keep+lists at gmail.com
Mon Jan 8 01:16:26 PST 2007


Marcin Kuszczak wrote:
> ohhh.. please... do not make another strange _context object!
> 
> Personally I think that passing information about variadic arguments of
> function with _arguments and _argptr is black side of D... To get it work
> properly IMHO there should be build in variant type (which is really
> usefull in some cases) and arguments to variadic functions could be sent in
> standard way:
> 
> void varfunction(variant[] vals ...); // Same syntax like D typesafe
> variadic functions.

I've never liked the "magic" _arguments and _argptr.  I always thought 
they should be explicitly named.

Then again, I don't think a variant type is the answer.  We have 
std.boxer.Box, but I think there must be a better solution for variadic 
functions.  Perhaps some special construct for talking about a 
function's call stack in a portable way...

> In case of template instatiation context the idea is nice and very usefull,
> but I would rather propose property like syntax for getting context:
> 
> void trace(T_Args...)(T_Args args...) {
>         writef("%s:%d (%s): ", trace.context.file, trace.context.line,
> trace.context.symbol);
>         foreach( arg ; args )
>                 writef(arg);
>         writefln();
>         }

> int foo()()
> {
> 	return foo.context.line;
> }

What does that do?  If you can't see what I'm getting at, try this:

 > class Foo
 > {
 >     int line()()
 >     {
 >         return line.context.line;
 >     }
 > }

Since we can "call" a parameter-less function without putting in the 
parens, how do we get to the context?  Making trailing '.'s suppress the 
function call would make the above style functions even less like real 
lvalues...

> Advantages:
> 1. Properties for functions already works (nothing interesting, but compiles
> without problem with DMD), so it is nothing new for compiler.
> 2. It could work also for normal functions
> 3. Makes less pollution of namespace

The above are true, but I think the syntax could be borderline. 
Originally I thought that there could be some kind of special argument 
in the template...

 > template tFoo(tArg1, tArg2, context) { }

But it just seemed... icky.  Hmm... what about...

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

How does that one sit?

	-- Daniel



More information about the Digitalmars-d mailing list