Deprecating this(this)

Jonathan M Davis newsgroup.d at jmdavisprog.com
Mon Apr 2 08:35:31 UTC 2018


On Monday, April 02, 2018 11:04:08 Shachar Shemesh via Digitalmars-d wrote:
> On 02/04/18 10:45, Jonathan M Davis wrote:
> > Honestly, I think at this point pure is easier to understand if you
> > think of it as @noglobal and don't think about functional purity at
> > all.
>
> That's fine. My point was that the only optimizations possible are
> possible on strictly pure functions (the immutable cast one included).
> Weakly pure functions add nothing.
>
> But merely having them around means that when I annotate a function with
> "pure", I do not promise any guarantees that the compiler can actually
> use to perform optimizations.

It means that a strongly pure function could call the function, which is the
entire reason that the definition of pure was widened to simply mean that it
guarantees that the function doesn't access global, mutable state instead of
also including the requirements about parameters which are placed on
strongly pure functions. So, a weakly pure function _can_ help with
optimizations in that it helps to implement strongly pure functions, and
without weakly pure functions, what you can do with strongly pure functions
can be very limited, making weakly pure functions very important even if all
you care about is optimizations, but no, a call to a weakly pure function
cannot be elided based on the fact that it's weakly pure. Now, pure combined
with const could provide some optimizations in rare cases, since the
compiler can guarantee that a pure function doesn't mutate a const argument
via another reference if no such mutable reference could be accessed through
one of the arguments, but I doubt that such optimizations are done at this
point, and it wouldn't involve eliding function calls. But in theory, the
fact that the compiler knows that a function can't access anything except
through its arguments could allow the compiler to optimize some code, even
if it doesn't involve eliding function calls.

Ultimately, I think that it's a mistake to think about pure having much to
do with optimizations. Much as such optimizations do exist, they're just too
limited. The primary advantage is that when you see that a function is pure,
you know that the function is just using what it's given via its arguments,
just like when you see a variable is immutable, you know that it can't be
mutated. Any optimizations that can be gotten via pure are therefore mostly
just gravy.

So, if your only motivation in dealing with pure is optimizations, then
there's a good chance that it really isn't worth your time, but personally,
I think that it's quite valuably simply for guaranteeing the that function
doesn't access global, mutable state.

- Jonathan M Davis



More information about the Digitalmars-d mailing list