digitalmars.D.learn - Convert call to a string
- data pulverizer (14/14) Feb 15 2017 I'd like to convert a call to a string for debug printing
- H. S. Teoh via Digitalmars-d-learn (11/27) Feb 15 2017 [...]
- H. S. Teoh via Digitalmars-d-learn (15/23) Feb 15 2017 [...]
- ag0aep6g (10/16) Feb 15 2017 `[args]` doesn't work when the tuple elements don't have a common
- H. S. Teoh via Digitalmars-d-learn (6/27) Feb 15 2017 Excellent idea!
- data pulverizer (3/3) Feb 15 2017 On Wednesday, 15 February 2017 at 22:07:22 UTC, data pulverizer
I'd like to convert a call to a string for debug printing purposes for example: ``` import std.stdio : writeln; void someFunction(int x, string y){} string myCall = debugPrint(someFunction(1, "hello")); writeln(myCall); ``` writes: someFunction(1, "hello") Does this functionality exists? If not how can I construct it? Please note that the call `someFunction(1, "hello")` should also be executed. Thank you
Feb 15 2017
On Wed, Feb 15, 2017 at 10:07:22PM +0000, data pulverizer via Digitalmars-d-learn wrote:I'd like to convert a call to a string for debug printing purposes for example: ``` import std.stdio : writeln; void someFunction(int x, string y){} string myCall = debugPrint(someFunction(1, "hello")); writeln(myCall); ``` writes: someFunction(1, "hello") Does this functionality exists? If not how can I construct it? Please note that the call `someFunction(1, "hello")` should also be executed.[...] Try this: auto debugPrint(string expr)() { writeln(expr); return mixin(expr); } string myCall = debugPrint!`someFunction(1, "hello")`; T -- Klein bottle for rent ... inquire within. -- Stephen Mulraney
Feb 15 2017
On Wed, Feb 15, 2017 at 02:18:48PM -0800, H. S. Teoh via Digitalmars-d-learn wrote: [...]Try this: auto debugPrint(string expr)() { writeln(expr); return mixin(expr); } string myCall = debugPrint!`someFunction(1, "hello")`;[...] OTOH, that won't work with local variables (it'd only print the variable names, not the values). Presumably you'd also want to print the variables too. So perhaps something like this instead: 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); T -- I see that you JS got Bach.
Feb 15 2017
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. 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); ----
Feb 15 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:Very good point.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.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
Feb 15 2017
On Wednesday, 15 February 2017 at 22:07:22 UTC, data pulverizer wrote: That's great, thanks both of you!
Feb 15 2017