Function attribute best practices

Ali Çehreli acehreli at
Mon Sep 12 17:08:29 UTC 2022

On 9/12/22 09:48, H. S. Teoh wrote:

 >> @nogc nothrow pure @safe
 >> unittest
 >> {
 >>      // ...
 >> }
 >> No, it isn't because unless my unittest code is impure, I can't catch
 >> my incorrect 'pure' etc. on my member functions.
 > [...]
 > Sure you can.  The `pure unittest` code obviously must itself be pure
 > (otherwise it wouldn't compile). If Foo introduces impure behaviour,
 > then the unittest, being pure, wouldn't be allowed to call Foo's impure
 > methods, which is what we want.  What's the problem?

There was a problem until you and others put me straigth. :)

What I meant was

- if I put 'pure' etc. on my templatized code,

- and then tested with a 'pure' unittest,

I wouldn't know that the gratuitous use of my 'pure' on the member 
function was wrong. I would be fooling myself thinking that I smartly 
wrote a 'pure' member function and a 'pure' unittest and all worked. 
Wrong idea! :)

Now I know I must leave attributes as much to inference as possible.


More information about the Digitalmars-d-learn mailing list