[Issue 2625] Creating new struct with literal bypasses immutability of members if struct is in array

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Apr 3 07:09:40 PDT 2009


http://d.puremagic.com/issues/show_bug.cgi?id=2625





------- Comment #4 from smjg at iname.com  2009-04-03 09:09 -------
Here's how it would have to work.

Really, there are four constancy levels:
- reassignable (the default)
- mutable but non-reassignable (MBNR)
- const
- invariant (immutable)

For primitive types and static arrays thereof, only two of these are distinct:
reassignable and invariant.  If something of such a type is declared const, it
actually becomes invariant.

The constancy of a struct is determined by two factors: the constancy of its
members and any constancy attributes with which the struct as a whole is
declared.

The constancy of a struct as determined by its members works like this:
- if all members are reassignable, it is reassignable
- if all members are invariant, it is invariant
- if all members are const, or all members are const or invariant, it is const
- otherwise, it is MBNR

The otherwise is if the struct has a mixture of reassignable and const and/or
invariant members, or has any MBNR members.  Or equivalently, if struct members
of structs are flattened out, the overall struct is MBNR iff there is a mixture
of reassignable and const and/or invariant members.

The essence of MBNR is that the struct cannot be reassigned, but the constancy
levels of the struct's members shine through.

Of course, it would still be possible to declare a struct 'variable' as const
or invariant, and this would be a matter of tightening the constancy from that
which is in the type.


-- 



More information about the Digitalmars-d-bugs mailing list