immutability and become (Was: Re: [squeak-dev] immutability)

Bert Freudenberg bert at freudenbergs.de
Thu Mar 18 12:32:46 UTC 2010


On 18.03.2010, at 06:37, Igor Stasenko wrote:
> 
> On 18 March 2010 06:51, Markus Lampert <markuslampert at yahoo.com> wrote:
>> I would have assumed that an immutable object cannot become anything else. Neither any other object, nor another immutable object. If you can change the state of an object in any way, either by changing it's state directly or by ex-changing it with another object, I would not consider it 'immutable'.

Right - if the state of the object is changeable by any means, it's not immutable. But if you make it immutable, "becomes" of the objects it references would fail. If your object is mutable but refers to immutable objects, they can still be changed by become. This is not changing the immutable objects, only the objects referring to them.

> Good. This makes it more consistent , except that ex-changing with
> another object notion.
> 
> So, you saying that i can do:
> 
> array := Array with: foo beImmutable.
> array at: 1 put: nil.
> 
> but can't do:
> 
> array := Array with: foo beImmutable.
> foo becomeForward: nil.

No, both of these would work. What you can't do is storing *into* an immutable object.

#beImmutable needs to be recursive. An immutable object cannot contain a reference to an immutable object.

Then you just disallow any stores into the immutable object, including becomes:

immutable := (Array with: 'hi') beImmutable.
regular := Array with: 'hi' beImmutable.

immutable first become: 'lo'. "fails"
regular first become: 'lo'. "succeeds"

- Bert -





More information about the Squeak-dev mailing list