Thread overview
How to create a function that behaves like std.stdio.writeln but prepends output with __FILE__:_LINE_
Jan 25, 2022
JG
Jan 25, 2022
Dennis
Jan 25, 2022
WebFreak001
Jan 25, 2022
JG
January 25, 2022

Any ideas how one can achieve what is written in the subject line?

f below does achieve this but I one would expect that it produces
significant template bloat.

g achieves the aim but isn't very elegant.

import std;
void f(string file=__FILE__,size_t line=__LINE__,R...)(R r)
{
  writeln(file,":",line," ",r);
}
void g(T)(T t,string file=__FILE__,size_t line=__LINE__)
{
     writeln(file,":",line," ",t.expand);
}

void main()
{
   f("hello ", 123);
   g(tuple("hello ",123));
}
January 25, 2022

On Tuesday, 25 January 2022 at 12:11:01 UTC, JG wrote:

>

Any ideas how one can achieve what is written in the subject line?

void f(T...)(auto ref T args, string file = __FILE__, int line = __LINE__)
{
    writeln(file, ":", line, ": ", args);
}
January 25, 2022

On Tuesday, 25 January 2022 at 12:27:16 UTC, Dennis wrote:

>

On Tuesday, 25 January 2022 at 12:11:01 UTC, JG wrote:

>

Any ideas how one can achieve what is written in the subject line?

void f(T...)(auto ref T args, string file = __FILE__, int line = __LINE__)
{
    writeln(file, ":", line, ": ", args);
}

note: default arguments after variadic arguments are supported since D 2.079.0: https://dlang.org/changelog/2.079.0.html#default_after_variadic

January 25, 2022

On Tuesday, 25 January 2022 at 12:27:16 UTC, Dennis wrote:

>

On Tuesday, 25 January 2022 at 12:11:01 UTC, JG wrote:

>

Any ideas how one can achieve what is written in the subject line?

void f(T...)(auto ref T args, string file = __FILE__, int line = __LINE__)
{
    writeln(file, ":", line, ": ", args);
}

Thank you very much.