immutable struct/class is mutable!

Jonathan M Davis jmdavisProg at gmx.com
Mon Jul 15 19:38:20 PDT 2013


On Tuesday, July 16, 2013 03:46:06 JS wrote:
> On Tuesday, 16 July 2013 at 01:24:37 UTC, Jonathan M Davis wrote:
> > On Monday, July 15, 2013 21:08:03 Dicebot wrote:
> >> On Monday, 15 July 2013 at 18:39:08 UTC, JS wrote:
> >> > and immutability doesn't nest. immutable struct A { struct B
> >> > {
> >> > }}, struct B is mutable.
> >> 
> >> What I have meant by "may be intended behavior" is that
> >> immutable
> >> qualifier does not attach at aggregate definitions. At all. It
> >> is
> >> irrelevant to the fact if B is nested or not.
> > 
> > Yes. Attributes such as immutable or private have no effect on
> > structs or
> > classes, just their members. It's a bit weird that way, but
> > that's the way
> > that it works.
> 
> Then we should be able to make a struct immutable itself. e.g.,
> immutable immutable(struct) A makes both A and it's members
> immutable. An immutable struct makes nested structs also
> immutable.

The way it works is that immutable on a struct should make anything within 
that struct (member variables, member functions, etc.) immutable. That should 
include nested members but apparently does not currently due to a bug. That 
behavior makes perfect sense to me. Aside from the bug fix, I don't understand 
what you're trying to gain here. As far as immutability goes, this behavior 
seems perfectly fine to me. I just find it a bit weird with regards to private, 
since it makes it so that you can't actually make a struct or class private - 
just its members. But since you can't do anything to it without accessing its 
members, AFAIK that behavior doesn't actually cause any problems.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list