Object.toString, toHash, opCmp, opEquals

Walter Bright newshound2 at digitalmars.com
Fri Apr 26 06:44:03 UTC 2024


Perhaps I can help things work for you and Timon:

```
import std.stdio;

class A
{
     string xxx(const Object) const { return "A"; }
}

class B : A
{
     alias xxx = A.xxx;
     string xxx(Object) { return "B"; }
}

void main()
{
     const A a = new A();
     B b = new B();
     const B c = new B();
     writeln(a.xxx(a));
     writeln(b.xxx(b));
     writeln(c.xxx(c));
}
```
I'm calling this xxx instead of toString, just so I can show all the code. 
Compiling it and running it prints:

A
B
A

In other words, you can have a toString() that is mutable and it will work fine 
with writeln(), because writeln(x) does not look for Object.toString(), it looks 
for x.toString().

Does this work for you?


More information about the Digitalmars-d mailing list