What is the utility of .stringof with expressions?

Chris Wright via Digitalmars-d digitalmars-d at puremagic.com
Sat Dec 12 08:49:56 PST 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.


More information about the Digitalmars-d mailing list