Yet Another Const Proposal

Janice Caron caron800 at googlemail.com
Fri Dec 7 12:16:20 PST 2007


On 12/7/07, Sönke Ludwig <ludwig at informatik_dot_uni-luebeck.de> wrote:
> Also, an important observation:
<snip>
> So to make the system semantically consistent, you'd have to forbid structure
> assignments, if there are any invariant members.

He's got a point. Here's a more drastic example.

    class A
    {
        invariant(char)[5] a;
    }

    A a;
    a = A("hello");
    string s = a.a;
    writefln(s); // prints "hello"
    a = A("world");
    writefln(s); // prints "world"

Whoops! The assignment of a seems to modified s ... an invariant!

I haven't actually tried to compile this, so it might not work.
(Anyone want to try it?). But Sönke's rule is sound: if a struct
contains an invariant (or, I would add, const) member, then assigning
the struct should be ruled out.




More information about the Digitalmars-d mailing list