I see. But is there any practical advantage of a function being pure? I mean, besides an optimization hint for the compiler, of course.
On Tue, Oct 25, 2011 at 10:41 PM, Jonathan M Davis <[email protected]> wrote: > On Tuesday, October 25, 2011 10:34 Gor Gyolchanyan wrote: >> I thought pure functions can't modify their parameters (along with any >> other non-local variables). >> if the pure function can't modify anything non-local, why should it >> care whether it's getting called from a shared context or not? > > No. The _only_ direct effects that pure has on a function is that it can't > access any module-level or static variables which can ever be mutated over the > course of the program (so, if they're immutable or if they're const value > types, then they can be used, but otherwise not) and that it can't call impure > functions. Previously, it was required that all of the parameters to a pure > function be either immutable or implicitly convertible to immutable (and I > believe that that's what TDPL states), which would make it impossible to alter > the function's arguments, but the result was that pure was too restrictive to > be of much use. So, it was changed. > > Now, any function which doesn't access any module-level or static variables > which can be modified during the course of the program and which doesn't call > any impure functions can be pure, but unlike before, purity is not enough for > a function call to be optimized out when it's called multiple times within an > expression. It can only be optimized out when the compiler can guarantee that > none of the function's arguments can be altered by that pure function, which > currently means that only functions whose arguments are all either immutable > or implicitly convertible to immutable can be optimized in that manner - just > like it was before. However, this makes it possible to have far more functions > be pure, so pure functions which do have parameters which are immutable or > implicitly convertible to immutable can call many more functions and do much > more. Being able to optimize out based on purity is therefore purely a > compiler optimization which it does when it can instead of always applying > to pure functions. > > - Jonathan M Davis >
