www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Convert call to a string

reply data pulverizer <data.pulverizer gmail.com> writes:
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
next sibling parent "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
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
prev sibling next sibling parent reply "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
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
parent reply ag0aep6g <anonymous example.com> writes:
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
parent "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
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
Feb 15
prev sibling parent data pulverizer <data.pulverizer gmail.com> writes:
On Wednesday, 15 February 2017 at 22:07:22 UTC, data pulverizer 
wrote:

That's great, thanks both of you!
Feb 15