any news on const/invariant?
Janice Caron
caron800 at googlemail.com
Thu Nov 29 01:11:34 PST 2007
On Nov 29, 2007 8:50 AM, Bruce Adams <tortoise_74 at yeah.who.co.uk> wrote:
> What would be the point of an invariant method? A const method promises not
> to change any contents of the object. An invariant method makes the same
> promise.
> So there is no difference to the caller of such a method.
> I can't see that it make any difference inside the method either.
It makes a difference. Watch...
class A
{
int x;
const int f() { return x; }
invariant int g() { return x; }
}
A ma = A(3);
writefln(ma.f()); /* error */
writefln(ma.g()); /* error */
const A ca = A(3);
writefln(ca.f()); /* OK */
wriitefln(ca.g()); /* error */
invariant A ia = A(3);
writefln(ca.f()); /* OK */
writefln(ia.g()); /* OK */
More information about the Digitalmars-d
mailing list