override to!string

Jesse Phillips jessekphillips+D at gmail.com
Fri Feb 25 07:31:45 PST 2011


spir Wrote:

> Hello,
> 
> Imagine I have the following custom type:
> 	alias float[] Numbers;
> 
> Is it possible to override to!string for Numbers so that it outputs eg like;
> 	(1.1 2.2 3.3)
> ?

No, this is one reason for writeTo replacing toString, or whatever the name. It would allow a formatter specified. I don't really know how to! would be changed to fit into that.


> I can indeed override/specialise toImpl or formatValue for the custom type, but 
> those overrides are simply ignored (tested).

Right, that would be anti-hijacking at work. Don't want behavior changed because you imported a module. You might be able to override to itself though.

Otherwise you can use format specifiers from std.string.format.


More information about the Digitalmars-d-learn mailing list