Object.toString, toHash, opCmp, opEquals

Walter Bright newshound2 at digitalmars.com
Fri Apr 26 20:17:09 UTC 2024


D1 is an example of a language with no attributes and no const. D1 works as a 
good programming language.

But it gives the programmer no indication of whether the arguments get mutated 
or not. He'll have to read and understand the called function, as well as the 
functions it calls.

It is reasonable to use const parameters when the argument is not going to be 
mutated. I personally prefer to use that as much as possible, and I like very 
much that the compiler will enforce it. With the mutating 4 functions, I cannot 
use const class objects.

Mutating toString, toHash, opCmp, and opEquals is unusual behavior, which is why 
const should be the default for them. After all, who expects a==b to change a or b?

I showed how to use the toString, toHash, opCmp, and opEquals functions with 
objects that do want to use mutating implementations of those functions. It will 
also be clear to the user which toString is mutating and which is not. It 
satisfies the use cases Timon mentioned - he'll still be able to use a mutating 
toString that will be used by writeln().



More information about the Digitalmars-d mailing list