The disadvantage of purity

berni44 dlang at d-ecke.de
Sun Dec 29 10:02:48 UTC 2019


I recently looked at issue 3821 (detecting recursive data 
structures in std.format to avoid a crash) [1]. I think, it could 
be easily fixed by collecting pointers to objects where toString 
is called and preventing a second call. Unfortunately (some 
instances of) format would not be pure anymore and this entails a 
lot of other functions which would loose it's purity. (And it 
doesn't work in CTFE, but that's an other story.)

The weird thing is, that the original call to the function 
(formatObject in std.format) would behave like a pure function: 
The result is always the same and the non local changes are 
temporary and not visible from the outside. The problem is, that 
this function should indeed return something different, when it 
calls itself (indirectly) with identical parameters once more to 
prevent the original call from never returning...

I have not yet a deep understanding of purity, so maybe there is 
a way arround this problem I cannot see due to my limited 
knowledge. Any ideas?

And, maybe somewhat heretical: Does the advantage of purity here 
really outweight the problems it causes?

[1] https://issues.dlang.org/show_bug.cgi?id=3821


More information about the Digitalmars-d mailing list