[Issue 17593] __POS__ magical constant like __FILE__ and friends
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Wed Jul 5 00:46:05 PDT 2017
https://issues.dlang.org/show_bug.cgi?id=17593
--- Comment #2 from Eyal <eyal at weka.io> ---
I tested the mixin approach before filing this. It doesn't work:
struct Pos { size_t line; }
void foo(size_t line=__LINE__, Pos pos=Pos(__LINE__))() { .. }
line is caller's line. pos.line is foo's decl line.
The reason I want this enhancement is that we have a logging framework that
uses the line numbers in compile-time (to make fast logs that log very
minimally at runtime).
I often want to create log-line wrappers for specific cases.
Currently I have to do this:
void myLogger(string fmt, string file=__FILE__, string mod=__MODULE__, size_t
line=__LINE__, Args...)(auto ref Args args) {
log!("my extra stuff" ~ fmt, file, mod, line)(myExtraArgs, args);
}
And this has to be repeated, verbatim, for every log wrapper in existence.
It is quite a big discouragement against writing functions that work and pass
on code positions.
Much nicer:
void myLogger(string fmt, Pos pos=__POS__) {
log!("my extra stuff" ~ fmt, pos)(myExtraArgs, args);
}
Almost as nice:
void myLogger(string fmt, Pos pos=mixin(POS)) ...
So alternatively to adding __POS__, dmd could be fixed so that __LINE__ is
based on its *lexical* position. i.e: same value for __LINE__ in a default CT
param value and when it is given to Pos() inside a default CT param value.
--
More information about the Digitalmars-d-bugs
mailing list