digitalmars.D - What is the utility of .stringof with expressions?
- Shriramana Sharma (18/18) Dec 12 2015 D currently supports:
- cym13 (4/22) Dec 12 2015 It could be useful combined with mixins to preprocess the code
- Shriramana Sharma (6/8) Dec 12 2015 Huh? Can you give me a concrete example? I'm not being intentionally den...
- ZombineDev (11/17) Dec 12 2015 If you know exactly what type you are using, then yes using
- Chris Wright (18/27) Dec 12 2015 stringof is useful for certain types of metaprogramming. It's useful for...
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
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
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
On Saturday, 12 December 2015 at 14:06:57 UTC, Shriramana Sharma wrote:cym13 wrote: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.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
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