www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Meaning of pure member function

reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
The following code compiles without error:

	class C {
		int x;

		// what does 'pure void' mean??
		pure void f() {
			x++;		// why is this legal?
		}
	}

What does 'pure' mean when applied to a member function? Based on
Andrei's book, 'pure' means that the function's result depends only on
its input. And based on the fact this code compiles, I deduced that
'this' is included as part of the function's input.

However, the function is clearly changing one of its inputs (changing a
member of 'this'). Furthermore, what on earth is 'pure void' supposed to
mean and why does the compiler accept it?

Changing the function to read:

	pure int f() { return x++; }

also compiles without any complaint from the compiler. Yet calling
writeln() from within f() produces an error. Why?


T

--
Computerese Irregular Verb Conjugation: I have preferences.  You have
biases.  He/She has prejudices. -- Gene Wirchenko
Jan 16 2012
parent reply "Jesse Phillips" <jessekphillips+D gmail.com> writes:
On Tuesday, 17 January 2012 at 05:16:33 UTC, H. S. Teoh wrote:
 The following code compiles without error:

 	class C {
 		int x;

 		// what does 'pure void' mean??
 		pure void f() {
 			x++;		// why is this legal?
 		}
 	}

 What does 'pure' mean when applied to a member function?
This is a weakly pure function usable by strongly pure functions. Namely it is a helper function to those that can claim to be strongly pure. Maybe bearophile's blog will shed some light: http://leonardo-m.livejournal.com/99194.html Or stackoverflow: http://stackoverflow.com/questions/5812186/pure-functional-programming-in-d
 Furthermore, what on earth is 'pure void' supposed to
 mean and why does the compiler accept it?
Well it can only be useful as a weakly pure function as those are allowed to modify their arguments. In any case, if the function was strongly pure: pure void foo() {} any call to it would just be eliminated as having no side effects.
Jan 16 2012
parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Tue, Jan 17, 2012 at 06:40:15AM +0100, Jesse Phillips wrote:
 On Tuesday, 17 January 2012 at 05:16:33 UTC, H. S. Teoh wrote:
The following code compiles without error:

	class C {
		int x;

		// what does 'pure void' mean??
		pure void f() {
			x++;		// why is this legal?
		}
	}

What does 'pure' mean when applied to a member function?
This is a weakly pure function usable by strongly pure functions. Namely it is a helper function to those that can claim to be strongly pure. Maybe bearophile's blog will shed some light: http://leonardo-m.livejournal.com/99194.html
[...] Thanks!! That is very insightful. So basically a strongly pure function cannot alter its arguments, but may create internal objects that are mutable as long as they never escape outside. Weakly pure functions are allowed to modify their arguments, but *only* whatever is reachable through their arguments and nothing else. So they can be called from a strongly pure function to operate on those internal objects because it's guaranteed that weakly pure functions never touch anything outside their arguments. This is indeed a very powerful concept. It greatly expands the scope of what can be implemented as a strongly pure function with all of its benefits -- memoization, optimization via factorization, etc.. Wow. Yet another reason to love D. :) Do the current D compilers implement memoization? T -- Shin: (n.) A device for finding furniture in the dark.
Jan 17 2012
parent reply "Jesse Phillips" <jessekphillips+D gmail.com> writes:
On Tuesday, 17 January 2012 at 16:07:08 UTC, H. S. Teoh wrote:
 Do the current D compilers implement memoization?
Not that I know of.
Jan 17 2012
parent Joshua Reusch <yoschi arkandos.de> writes:
Am 17.01.2012 17:19, schrieb Jesse Phillips:
 On Tuesday, 17 January 2012 at 16:07:08 UTC, H. S. Teoh wrote:
 Do the current D compilers implement memoization?
Not that I know of.
dmd not, but phobos: http://dlang.org/phobos/std_functional.html#memoize But as a library implementation, it is not applied automatically. PS: The dlang.org site appears to be faster as usual ! Great news :)
Jan 17 2012