Uh... destructors?

Steven Schveighoffer schveiguy at yahoo.com
Tue Feb 22 12:04:23 PST 2011


On Tue, 22 Feb 2011 14:31:30 -0500, %u <wfunction at hotmail.com> wrote:

> I just visited Wikipedia (savior of the day) and a quick look at
> this article:
> http://en.wikipedia.org/wiki/Pure_function
>
> yields the following requirements for a pure function:
>
> 1. The function always evaluates the same result value given the
> same argument value(s). The function result value cannot depend on
> any hidden information or state that may change as program execution
> proceeds or between different executions of the program, nor can it
> depend on any external input from I/O devices.
>
> 2. Evaluation of the result does not cause any semantically
> observable side effect or output, such as mutation of mutable
> objects or output to I/O devices.
>
> "pure" destructors always fail the first test, because they clearly
> cause a state change for the current object, if not for a global
> resource like memory. Pure functions need to be timeless and can be
> freely reordered; a destructor call, however, is not timeless -- it
> depends on the current state of the object, and the changes it makes
> are visible to the outside world.
>
> They also usually fail the second test, because if, for example, we
> flush a file inside a destructor, that clearly has an observable
> output... so I can't see why a destructor would ever be pure.

D pure functions are significantly different than this definition (as of  
recent times, when weak-pure was added).

Essentially, a pure function cannot access global variables.  However, it  
can access variables referred to via a member of the object instance.

i.e. this is a valid pure function:

class C
{
   int x;
   pure void foo() { x++; }
}

-Steve


More information about the Digitalmars-d mailing list