Convert call to a string
H. S. Teoh via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Feb 15 15:01:39 PST 2017
On Wed, Feb 15, 2017 at 10:58:42PM +0000, ag0aep6g via Digitalmars-d-learn wrote:
> On Wednesday, 15 February 2017 at 22:34:22 UTC, H. S. Teoh wrote:
> > auto debugPrint(alias fun, A...)(A args) {
> > writefln("%s(%(%s, %))", __traits(identifier, fun), [args]);
> > return fun(args);
> > }
> >
> > string arg = "hello";
> > string myCall = debugPrint!someFunction(1, arg);
>
> `[args]` doesn't work when the tuple elements don't have a common
> type.
Very good point.
> But you can pass `args` as is to writefln and generate the format
> string accordingly:
>
> ----
> import std.range: repeat;
> import std.string: join;
>
> immutable string argsfmt = "%s".repeat(args.length).join(", ");
> writefln("%s(" ~ argsfmt ~ ")", __traits(identifier, fun), args);
> ----
Excellent idea!
T
--
Life is complex. It consists of real and imaginary parts. -- YHL
More information about the Digitalmars-d-learn
mailing list