[squeak-dev] immutability

Michael van der Gulik mikevdg at gmail.com
Wed Mar 17 20:02:32 UTC 2010


On Thu, Mar 18, 2010 at 3:26 AM, Randal L. Schwartz
<merlyn at stonehenge.com> wrote:
>
> Has anyone looked at, proposed, implemented object immutability?
> Obviously, it would require a VM change.

It doesn't really need a VM change. I'll be implementing immutability
in SecureSqueak, and most (or all?) literals in a CompiledMethod will
be made immutable.

The pattern I use is that setters in immutable objects are implemented
like this:

MyObject>>someValue: x
    baked ifFalse: [ someValue := x ]
        ifTrue: [ throw an error or something. ].

"baked" is an instance variable.

And then:

MyObject>>initialize
    | result |
    result := super initialize.
    baked isNil ifFalse: [ ^ error of some sort. ].
    baked := false.
    ^ result.

MyObject>>bake
    baked := true. " i.e. not nil."

In SecureSqueak, direct invasive object access using basicAt:put:,
at:put: and so forth will be disallowed.

Gulik.

-- 
http://gulik.pbwiki.com/



More information about the Squeak-dev mailing list