Uh... destructors?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Wed Feb 23 08:45:48 PST 2011


On 2/23/11 9:57 AM, Steven Schveighoffer wrote:
> On Wed, 23 Feb 2011 10:46:43 -0500, Andrei Alexandrescu
> <SeeWebsiteForEmail at erdani.org> wrote:
>
>> At a level it's clear to me that a function cannot be at the same time
>> pure and unsafe. For example:
>>
>> pure void foo(int *x) { free(x); (*x)++; }
>>
>> This function essentially breaks any guarantee for the entire program,
>> so it would be quite difficult to claim it's pure.
>
> I thought @safe was orthogonal to pure? If this isn't the case, then
> yes, free must be disallowed. But then malloc must also be, and any
> construct which manages its own memory via malloc/free.

malloc can be reasonably made pure. It would be a mistake to see malloc 
and free as two symmetrical parts of a whole. The truth of the matter is 
that allocating is fine, deallocating is problematic.

>  From what I remember, pure functions:
>
> 1. cannot access shared or global non-immutable data
> 2. cannot call non-pure functions
>
> I don't remember the definition that pure functions must also be @safe.

Perhaps we should amend the definition.


Andrei


More information about the Digitalmars-d mailing list