Against enforce()

Steven Schveighoffer schveiguy at yahoo.com
Thu Mar 17 08:23:30 PDT 2011


On Wed, 16 Mar 2011 22:10:01 -0400, Andrei Alexandrescu  
<SeeWebsiteForEmail at erdani.org> wrote:

> On 03/16/2011 08:53 PM, bearophile wrote:
>> Andrei:
>>
>>> Yum, love the enforce.
>>
>> You are silly :-)
>>
>>
>>> So perhaps the language could be improved as enforce does not break  
>>> purity.
>>
>> Currently enforce() uses a lazy argument. A lazy argument is a  
>> delegate, and generally such delegate can't be pure, because the  
>> expressions you give to enforce() usually refer to variables in the  
>> scope where you call enforce(). So what kind of language improvements  
>> are you thinking about?
>
> I haven't thought through it. Clearly this is a false positive, a  
> restriction that should be at best removed.

int x;

void foo(int i)
{
    enforce(i == 5, to!string(x = 5)); // if enforce is pure, this will  
mean the global x could be affected inside the function
}

I don't think enforce can be straight pure.  It can only be pure when  
called from pure functions.  A so-called conditional purity.

If we have auto purity, then I think the compiler could sort this out.

-Steve


More information about the Digitalmars-d mailing list