A problem with mutable toString

H. S. Teoh hsteoh at quickfur.ath.cx
Sun Apr 8 18:02:01 PDT 2012


On Mon, Apr 09, 2012 at 02:25:56AM +0200, bearophile wrote:
[...]
> While this gives errors:
> 
> class Foo {
>       override string toString() /*const*/ {
>           return "Foo";
>       }
> }
> void main() {
>       import std.stdio;
>       const Foo[] foos = [new Foo];
>       writeln(foos);
> }
> 
> 
> Output, DMD 2.059beta2:
> 
> ...\dmd2\src\phobos\std\format.d(2158): Error: template instance
> formatObject!(LockingTextWriter,const(Foo),char)
> formatObject!(LockingTextWriter,const(Foo),char) does not match
> template declaration formatObject(Writer,T,Char) if
> (hasToString!(T,Char))
[...]
> Is this good/expected?
[...]

IMO this is correct. The error message could be improved, but basically
the compiler is complaining that it can't find a suitable toString()
method to use. This is probably because Walter recently changed it so
that toString() must be const, which I think makes sense. Non-const
toString causes a lot of grief in druntime code (and probably elsewhere
too).


T

-- 
If you compete with slaves, you become a slave. -- Norbert Wiener


More information about the Digitalmars-d-learn mailing list