On Wednesday, October 26, 2011 11:15:20 Gor Gyolchanyan wrote: > I see. But is there any practical advantage of a function being pure? > I mean, besides an optimization hint for the compiler, of course.
1. You know that it doesn't access global variables, which is at minimum an advantage as far as understanding the code goes. 2. Under some situations, pure allows the compiler to know that the return value has no aliasing with the function's arguments, so the return value of the function can be implicitly converted to immutable. 3. Aside from the potential optimization of removing additional calls to the same function with the same arguments within a statement, the combination of pure and const on member variables gives stronger guarantees than either alone could, potentially further aiding compiler optimizations. There are probably others, but that's what comes to mind at the moment. Primarily though, it comes down to being able to guarantee a certain level of encapsulation (which makes it easier to reason and make guarantees about your program) and enables the compiler to better optimize code. - Jonathan M Davis