feature request: special optional argument (__FILE__, ...) AFTER variadic template
timotheecour
thelastmammoth at gmail.com
Fri Feb 1 03:00:45 PST 2013
currently there's a limitation with logging using variadic
templates:
eg:
the following is not allowed because it's ambiguous:
void DEBUG(T...)(T a, string file=__FILE__) {...}
proposal:
create a new kind of optional argument that's uninstantiable by
user code, only by the compiler. We could use the following new
syntax "==" instead of "=":
void DEBUG(T...)(T a, string file==__FILE__) {write(file,":");
foreach(ai ;a) write(ai," "); }
user code:
DEBUG(1+1, 2.2); //at file=__FILE__="myfun.d"
//this will print:"myfun.d:1+1 2.2"
the compiler would apply the same rules as for deducing the
variadic T in "void DEBUG(T...)(T a)" and append one more arg to
it (file). Note that file can't directly be instantiated by user
code, it has to be implicit, to avoid the ambiguity.
This would be also very helpful in conjunction with my previous
proposal earlier today ("feature request: __ARGS__ for logging
(cf __FILE__, __LINE__, __FUNC___)". One could simply write:
---
void DEBUG(T...)(T a, string[]args==__ARGS__){
foreach(ai,vai;lockstep(a,args) writeln(vai,"=",ai,"; ");
}
int a=3,b=4;
DEBUG(1+2,3*3,a,b);//prints 1+2=3; 3*3=9;a=3;b=4;
---
Plain dead simple logging that just works with any number of args
and avoids verbosity of tuple syntax; very useful for quick
debugging.
Note: we can have more such special optional args at the end:
void DEBUG(T...)(T a, string[]args==__ARGS__, string
file==__FILE__)
the limitation: they have to be implicitly convertible as above
by such macros.
More information about the Digitalmars-d
mailing list