Pure Contract bug?

David Held dmd at wyntrmute.com
Thu Jan 2 16:31:24 PST 2014


On 2/4/2012 12:45 PM, Timon Gehr wrote:
> [...]
> Pure does not imply const in D.
> [...]

I think this is a language defect:

struct Foo
{
     int computed() pure { return x * y; }
     int wrapper() const { return computed() + 5; }

     int x;
     int y;
}

void main()
{
}

src\Bug2.d(4): Error: mutable method Bug2.Foo.computed is not callable 
using a const object

Surprisingly, this is legal, and "fixes" the issue:

     int computed() const pure { return x * y; }

I say this is a bug because of what a "non-const pure" method would be: 
a method which could somehow modify 'this', or its members.  I hope we 
all agree that such a method is not, in fact, pure.  Let's try, just to 
make sure:

     int computed() pure { return ++x * y; }

Oh noes...this is allowed!!!  Surely this is wrong.  Suppose we rewrote 
the method like so:

     int computed(ref int x, int y) pure { return ++x * y; }

Would anyone say this is legit?

Dave



More information about the Digitalmars-d-learn mailing list