What is the utility of .stringof with expressions?

ZombineDev via Digitalmars-d digitalmars-d at puremagic.com
Sat Dec 12 07:20:47 PST 2015


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.



More information about the Digitalmars-d mailing list