A problem with mutable toString

Kenji Hara k.hara.pg at gmail.com
Mon Apr 9 21:15:57 PDT 2012


On Monday, 9 April 2012 at 00:25:57 UTC, bearophile wrote:
> Currently this code compiles and runs with no 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:
>
> [Foo]
>
>
>
> 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:
>
[snip]

Current std.format doesn't support const class formatting with 
upcasting to mutable Object and using mutable toString().

> Is this good/expected?

It is a gap between language const system and object.Object class 
interface.
I think Phobos should not break const system in basic.

Kenji Hara


More information about the Digitalmars-d-learn mailing list