Assert allowed to have side effects?

David Nadlinger see at klickverbot.at
Mon May 30 17:09:30 PDT 2011


On 5/31/11 1:34 AM, Stewart Gordon wrote:
> On 29/05/2011 14:03, bearophile wrote:
>> Stewart Gordon:
>>
>>> There are places where the spec fails to make a clear distinction
>>> between illegal code and
>>> incorrect code that the compiler may reject if it's smart enough.
>>
>> In D there are pure functions, so I think it's not too much hard for
>> it to tell apart
>> when the contents of an assert() are pure or not.
>> My opinion is that the D compiler has to enforce purity inside
>> assert(), to avoid bugs.
>
> Only if purity rules are relaxed. AIUI, one of the restrictions at the
> moment is that in a pure function only immutable data can be accessed.
> As long as this restriction remains in place, adding the restriction of
> purity to asserts would erode their usefulness.
>
> Stewart.

Nope, since the new, relaxed purity rules (originally proposed by Don, 
IIRC) were implemented in DMD 2.050, this is no longer the case. »Weakly 
pure« functions take mutable parameters and can access them (this also 
applies to the hidden this reference), while »strongly pure« functions 
only operate on immutable values. There is no syntactic distinction 
between them, both are annotated by the »pure« keyword, it just depends 
on the parameter types.

David


More information about the Digitalmars-d-learn mailing list