www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - What is pure used for?

reply sclytrack <fake hotmail.com> writes:
My understanding is that pure is used for compiler optimization 
in loops and expressions. It leaves out multiple calls if it 
figures out it is not needed.

Is pure used for anything else?


int * pureFunction()

1) the pointer needs to be the same.
2) the value that the pointer points to needs to be the same. I 
call this the "value
of interest" with relation to pure. The pointer doesn't matter.
3) both the pointer and the value the pointer is pointing too 
needs to be the same.

pureCalloc() satisfies (2)
pureMalloc() violates everything.

Does the D compiler do any optimizations?
Nov 24 2021
next sibling parent Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Thursday, 25 November 2021 at 07:26:48 UTC, sclytrack wrote:
 My understanding is that pure is used for compiler optimization 
 in loops and expressions. It leaves out multiple calls if it 
 figures out it is not needed.

 Is pure used for anything else?


 int * pureFunction()

 1) the pointer needs to be the same.
 2) the value that the pointer points to needs to be the same. I 
 call this the "value
 of interest" with relation to pure. The pointer doesn't matter.
 3) both the pointer and the value the pointer is pointing too 
 needs to be the same.

 pureCalloc() satisfies (2)
 pureMalloc() violates everything.

 Does the D compiler do any optimizations?
https://dlang.org/spec/function.html#pure-functions
Nov 25 2021
prev sibling next sibling parent zjh <fqbqrr 163.com> writes:
On Thursday, 25 November 2021 at 07:26:48 UTC, sclytrack wrote:
 My understanding is that pure is used for compiler optimization 
 in loops and expressions. It leaves out multiple calls if it 
 figures out it is not needed.
[one link](https://klickverbot.at/blog/2012/05/purity-in-d/#indirections in-the-return-type) and [another](https://theartofmachinery.com/2016/03/28/dirtying_pure_functions_can_be_useful.html)
Nov 25 2021
prev sibling parent Paul Backus <snarwin gmail.com> writes:
On Thursday, 25 November 2021 at 07:26:48 UTC, sclytrack wrote:
 int * pureFunction()

 1) the pointer needs to be the same.
 2) the value that the pointer points to needs to be the same. I 
 call this the "value
 of interest" with relation to pure. The pointer doesn't matter.
 3) both the pointer and the value the pointer is pointing too 
 needs to be the same.

 pureCalloc() satisfies (2)
 pureMalloc() violates everything.
There is a special case in the language spec to allow functions like `pureMalloc`. They are called "pure factory functions":
 A *pure factory function* is a strongly pure function that 
 returns a result that has mutable indirections. All mutable 
 memory returned by the call may not be referenced by any other 
 part of the program, i.e. it is newly allocated by the 
 function. Nor may the mutable references of the result refer to 
 any object that existed before the function call.
Source: <https://dlang.org/spec/function.html#pure-functions> (scroll down)
 Does the D compiler do any optimizations?
Yes, but those optimizations may result in implementation-defined behavior:
 **Implementation Defined:** An implementation may assume that a 
 strongly pure function that returns a result without mutable 
 indirections will have the same effect for all invocations with 
 equivalent arguments. It is allowed to memoize the result of 
 the function under the assumption that equivalent parameters 
 always produce equivalent results. [...] An implementation is 
 currently not required to enforce validity of memoization in 
 all cases.
The term "equivalent" is not explicitly defined, but as far as I can tell it means something like item (2) in your list.
Nov 25 2021