www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - What is the utility of .stringof with expressions?

reply Shriramana Sharma <samjnaa_dont_spam_me gmail.com> writes:
D currently supports:

writeln((1 + 2).stringof);

to print:

1 + 2

What is the real-world use case of this "feature"? I mean, everyone knows 
what the code they write looks like, so why would they want to have a 
language feature to get a string representation of it that they can print 
out to the user? I mean, if at all someone wants to print out 1 + 2, they 
can always say "1 + 2" and be done with it, instead of going to the 
convolution of .stringof...

One thing I observe however is that:

    writeln((1+ 2).stringof);
    writeln((1 +2).stringof);
    writeln((1  +  2).stringof);

all print "1 + 2" (without the quotes) so it's not a simple compiler dumps 
to string thing, but still I don't understand what this can be useful for...

-- 

Dec 12 2015
next sibling parent reply cym13 <cpicard openmailbox.org> writes:
On Saturday, 12 December 2015 at 10:20:01 UTC, Shriramana Sharma 
wrote:
 D currently supports:

 writeln((1 + 2).stringof);

 to print:

 1 + 2

 What is the real-world use case of this "feature"? I mean, 
 everyone knows what the code they write looks like, so why 
 would they want to have a language feature to get a string 
 representation of it that they can print out to the user? I 
 mean, if at all someone wants to print out 1 + 2, they can 
 always say "1 + 2" and be done with it, instead of going to the 
 convolution of .stringof...

 One thing I observe however is that:

     writeln((1+ 2).stringof);
     writeln((1 +2).stringof);
     writeln((1  +  2).stringof);

 all print "1 + 2" (without the quotes) so it's not a simple 
 compiler dumps to string thing, but still I don't understand 
 what this can be useful for...
It could be useful combined with mixins to preprocess the code you write before compiling it.
Dec 12 2015
parent reply Shriramana Sharma <samjnaa_dont_spam_me gmail.com> writes:
cym13 wrote:

 It could be useful combined with mixins to preprocess the code
 you write before compiling it.
Huh? Can you give me a concrete example? I'm not being intentionally dense but I can't imagine such a use case where you can't just write the string literal yourself... --
Dec 12 2015
parent ZombineDev <valid_email he.re> writes:
On Saturday, 12 December 2015 at 14:06:57 UTC, Shriramana Sharma 
wrote:
 cym13 wrote:

 It could be useful combined with mixins to preprocess the code 
 you write before compiling it.
Huh? Can you give me a concrete example? I'm not being intentionally dense but I can't imagine such a use case where you can't just write the string literal yourself...
If you know exactly what type you are using, then yes using .stringof may not be very interesting. However where .stringof like .tupleof really shine is generic programming. For example, see with how little effort you can implement JSON serialization for any type: http://dpaste.dzfl.pl/5a690bb1ce5a Whereas in other languages you would need to write tons of boilerplate like for example implementing a serialize() method in every single class that you want to be able to convert to JSON.
Dec 12 2015
prev sibling parent Chris Wright <dhasenan gmail.com> writes:
On Sat, 12 Dec 2015 15:50:01 +0530, Shriramana Sharma wrote:

 D currently supports:
 
 writeln((1 + 2).stringof);
 
 to print:
 
 1 + 2
 
 What is the real-world use case of this "feature"?
stringof is useful for certain types of metaprogramming. It's useful for providing error messages in any sort of template, and it's useful for mixin templates that construct code at compile time. Types are the major place where you benefit from this, but it also helps with template alias expressions. It's simpler for users to understand and for the spec if all expressions have .stringof. stringof for arbitrary expressions isn't so useful today. Right now, you can pass an expression as a template argument, but the expression is evaluated at compile time and then passed to the template. If you could pass the expression to the template without evaluating it, then there are some interesting things you can do. For instance, you could make verbose assertions that produce output like: Assertion failed: addTwo(addTwo(6)) != 6 + 6 -> addTwo(8) != 12 -> 10 != 12 without having to pass a string to a template.
Dec 12 2015