How purity w/o the specific optimizations then allowed is useful ?

Quirin Schroll qs.il.paperinik at gmail.com
Mon Apr 22 16:59:29 UTC 2024


On Tuesday, 16 April 2024 at 15:07:34 UTC, Basile B. wrote:
> Short story for the context : someone has recently proposed the 
> concept of purity for FreePascal, and only for their own 
> dialect (aka _ObjFPC_). The proposition is to "check purity 
> only when optimizations are enabled, because essentially the 
> point of purity is only to optimize more" (think CTFE, think 
> memoization).
>
> But [I fail to explain the 
> opposite](https://gitlab.com/freepascal.org/fpc/source/-/merge_requests/645#note_1863564617). How is purity a useful concept if not leading to specific optimizations ?
>
> And is that even the case ?
>
> Side note: I dont plan the use your answers to arguee w/ the 
> author. It's more that conceptually I'm interested.

In regards to the type system, depending on parameters, the 
result of a `pure` function may be assumed unique, which allows 
it to implicitly convert to `immutable`. (For an impure function, 
looking at the signature only, one cannot be determine if its 
result is unique as it could just return some mutable global.) 
This means that a `pure` function can build a structure 
containing indirections and whatnot and you, the receiver, may 
decide that it won’t be changed and therefore be treated as 
`immutable` (not just `const`), which in turn can either aid you 
in reasoning about your code and enable optimizations.


More information about the Digitalmars-d mailing list