Variable arguments with file and line information?

Jonathan M Davis jmdavisProg at gmx.com
Sun Nov 17 13:28:41 PST 2013


On Sunday, November 17, 2013 22:08:38 Rob T wrote:
> Good points, got it down to this.
> 
> void error(string a_Msg, string file = __FILE__, size_t line =
> __LINE__)
> {
>     writefln( a_Msg ~ ". In file %s on line %d.", file, line );
> }
> 
> int main()
> {
>     format("hallo").error;
>     format("Hallo %s.", "du da").error;
> }
> 
> There should be no more template bloat, and it looks to be about
> as usable and simple as possible.
> 
> Do you see any further optimizations that do not increase the
> usage convenience?

Yeah. Don't use concatenation in your format string:

writefln("%s. In file %s on line %s.", msg, file, line);

Other than that, it looks fine. Personally, I'd change it to

writefln("%s(%s): %s", file, line, msg);

but that's personal preference.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list