How do "pure" member functions work?
Don
nospam at nospam.com
Wed Aug 24 05:57:09 PDT 2011
Simen Kjaeraas wrote:
> On Mon, 22 Aug 2011 22:19:50 +0200, Don <nospam at nospam.com> wrote:
>
>> BTW: The whole "weak pure"/"strong pure" naming was just something I
>> came up with, to convince Walter to relax the purity rules. I'd rather
>> those names disappeared, they aren't very helpful.
>
> The concepts are useful, but better names might be worth it. But what
> short word eloquently conveys 'accesses no mutable global state'? :p
@nostatic ?
stateless ?
> What we call strongly pure is what in other languages is simply called
> 'pure', and that is likely the word that should be used for it.
> Weakly pure is a somewhat different beast, and the 'best' solution would
> likely be for it to be the default (But as we all know, this would
> require changing the language too much. Perhaps in D3...). Noglobal
> might be the best we have. My favorite thus far is 'conditionally pure'.
> It conveys that the function is pure in certain circumstances, and not
> in others. However, it might be somewhat diluted by the addition of pure
> inference in newer versions of DMD - that definitely is conditionally
> pure.
>
> Const pure is not a concept I'm particularly familiar with. Is this the
> special case of calling a conditionally pure function with only
> const/immutable parameters, with arguments that are immutable in the
> calling context, and that it in those cases can be considered
> strongly pure?
No, it's where the signature contains const parameters, rather than
immutable ones.
Two calls to a const-pure function, with the same parameters, may give
different results. You'd need to do a deep inspection of the parameters,
to see if they changed.
Consider:
x = foo(y);
x = foo(y);
where foo is const-pure.
Perhaps y contains a pointer to x. In that case, foo could depend on x.
Or, there might be a mutable pointer to y somewhere, and x might have an
opAssign which modifies y.
In each case, the second y is different to the first one.
But, if foo is immutable-pure, it will return the same value both times,
so one of the calls can be optimized away.
More information about the Digitalmars-d-learn
mailing list