Meaning of pure member function

H. S. Teoh hsteoh at quickfur.ath.cx
Tue Jan 17 08:08:32 PST 2012


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.


More information about the Digitalmars-d-learn mailing list