Extended Type Design.

Andrei Alexandrescu (See Website For Email) SeeWebsiteForEmail at erdani.org
Mon Mar 19 22:55:12 PDT 2007


Derek Parnell wrote:
> On Mon, 19 Mar 2007 22:34:13 -0700, Andrei Alexandrescu (See Website For
> Email) wrote:
> 
>> Tyler Knott wrote:
>>> *const and invariant only make sense when applied to references, 
>>> otherwise they are ignored or (preferably) errors.
>> Correct.
> 
> Can 'const' and 'invariant' apply to structs, especially ones that contain
> references.
> 
>   struct Foo
>   {
>       char[] s;
>       char c;
>   }
> 
>   const Foo f;  // Is this a mistake then?

That code is fine. I should have said "const and invariant make sense 
only when applied to types that are, or contain, references". Sorry.

>   f.s[0] = 'a'; // okay???

No.

>   f.s = "xyz";  // okay???

Yes.

>   f.s = "def".dup; // okay ???

Yes.

>   f.s.length = 1;  // okay?

Yes.

>   f.s ~= 'a'; // okay??

Yes.

>   f.c = 'a';   // okay???

Yes.

>   const Foo somefunc();
>   f = somefunc; // okay??

Yes.

>   Foo someotherfunc();
>   f = someotherfunc; // okay??

Yes. Non-const transforms into const no problem.


Andrei



More information about the Digitalmars-d mailing list