Thread overview
Re: Meaning of pure member function
Jan 17, 2012
H. S. Teoh
Jan 17, 2012
Jesse Phillips
Jan 17, 2012
Joshua Reusch
January 17, 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.
January 17, 2012
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.


January 17, 2012
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 :)