Object.toString, toHash, opCmp, opEquals

Walter Bright newshound2 at digitalmars.com
Sat Apr 27 01:45:44 UTC 2024


On 4/26/2024 2:38 PM, Dennis wrote:
> Timon has mentioned data structures with amortized time complexity several times 
> now, but perhaps an example closer to home helps:
> 
> https://github.com/dlang/dmd/blob/9ffa763540e16228138b44c3731d9edc2a7728b6/compiler/src/dmd/dsymbol.d#L668
> 
> In this case, `toString` (or `toPrettyChars`, same idea) is *logically* const 
> because it doesn't mutate the Dsymbol meaningfully, but it can't be D's *memory* 
> const because it does change a class field to store a cached result.
> 

I'm aware of that, and have investigated it several times looking for what can 
be made const. The compiler does a lot of lazy evaluation.

The simplest way to deal with that is to get the logical const value at the call 
site, then pass it to a const parameter.


More information about the Digitalmars-d mailing list