Store mutable indirections in immutable data with this one weird trick!

Paul Backus snarwin at gmail.com
Sat Nov 13 15:16:43 UTC 2021


On Saturday, 13 November 2021 at 15:01:08 UTC, Dukc wrote:
>> [1] Actually, the spec says that the compiler is allowed to 
>> assume referential transparency anyway, which turns this from 
>> an unfortunate limitation into a loaded foot-gun.
>
> Ah, you already realized what I just said above. Yeah, it's an 
> unfortunate trap but what do you do? Compiler optimisation is 
> based on assumptions and assumptions usually necessiate 
> pitfalls like this.

What you do is have the compiler enforce the preconditions 
necessary to make its assumptions hold. So, if the result of a 
`pure` function is not supposed to depend on the specific values 
of any pointers, `pure` functions should be forbidden from 
comparing pointers, casting them to integers, or performing any 
other operations that might introduce such a dependency.

Or, alternatively, if that's too much trouble, you recognize that 
the assumption underlying this optimization is invalid, and you 
remove it from the compiler and the language spec. (Which I 
expect is what will happen in practice, sooner or later.)


More information about the Digitalmars-d mailing list