logical const is a subset of transitive const

Daniel Keep daniel.keep.lists at gmail.com
Fri Sep 14 18:16:22 PDT 2007



Janice Caron wrote:
> [...]
> What I /don't/ understand is why D lets you declare a member function
> (as opposed to an object) as being invariant. To my brain, that ought
> to mean the function sees "this" as having the type invariant(T) - but
> that's clearly an invalid assumption, because "this" /can/ be
> modified. You have a read-only view, that's all.

I'd always assumed it was so you could do this:

class A
{
    int foo() { ... }
    invariant int foo() { ... }
}

{
    invariant(A) a = cast(invariant) new A;
    a.foo();
}

I realise that I could have accomplished the same thing by making the
second foo a const method, but the question is, is that always the same?
 I can't think of a case, but it's possible that sometimes the
implementation might change or be more efficient if you've got
invariant(this) instead of const(this).

	-- Daniel



More information about the Digitalmars-d mailing list