Object.toString, toHash, opCmp, opEquals

Walter Bright newshound2 at digitalmars.com
Thu Apr 25 23:06:27 UTC 2024


The prototypes are:

```
string toString();
size_t toHash() @trusted nothrow;
int opCmp(Object o);
bool opEquals(Object o);
```

which long predated `const`. The trouble is, they should be:

```
string toString() const;
size_t toHash() const @trusted nothrow;
int opCmp(const Object o) const;
bool opEquals(const Object o) const;
```

Without the `const` annotations, the functions are not usable by `const` objects 
without doing an unsafe cast. This impairs anyone wanting to write const-correct 
code, and also impedes use of `@live` functions.

I recommend that everyone who has overloads of these functions, alter them to 
have the `const` signatures. This will future-proof them against any changes to 
Object's signatures.


More information about the Digitalmars-d mailing list