__ARGS__ : allow access to (stringified) arguments, as C's `#arg` macro

Jacob Carlborg doob at me.com
Fri Jan 19 08:51:00 UTC 2018


On 2018-01-18 00:13, Timothee Cour wrote:
> I wrote something like that to mimic C's `#arg` preprocessor
> (stringify argument) for debugging functions, eg:
> 
> ```
> // simplified here:
> void log(string file=__FILE__, int line=__LINE__, T) (T a){
>     enum arg_stringified=import(file)[line]; // more complex in practice
>    writeln(arg_stringified, ":", a);
> }
> void main(){
>    log(1+3); // prints: `1+3:4`
> }
> ```
> 
> however: this slows down compilation a lot (in larger programs) and
> has potentially complex logic  to deal with multiple arguments, and
> UFCS, as we need to redo the job of the parser to get access to
> individual elements stringified
> we can avoid slowing down compilation time by passing pass file,line
> at runtime and use readText, has the defect of not working if code was
> compiled and source changed at a later time.
> 
> So I'd still like a solution that mimic's C's `#arg` preprocessor
> https://gcc.gnu.org/onlinedocs/gcc-4.0.0/cpp/Stringification.html ;
> it's sad that D is inferior to C in that respect.
> 
> what i would like instead is __ARGS__:
> thus allowing:
> ```
> void log(T...) (T a, string file=__FILE__, int line=__LINE__, string[]
> arg_names=__ARGS__){
>    writeln(file, ":", line, " ", zip(arg_names, a))); // or better formatting
> }
> 
> ```

Not sure I understand this feature. Is it something like:

auto foo = 3;
auto bar = 4;
log(foo, bar);

Would print?

main.d:3 foo=3
main.d:3 bar=4

If that's the case then this seems like yet another hack because we 
don't have AST macros.

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list