Search code examples
smalltalkvisualworksgnu-smalltalk

Smalltalk How to create immutable instance variable?


I have a class with an instance variable var.
I don't want the variable to be modified / assigned to a value except when the object is created using a Class method.

isImmutable: aBoolean is the method to convert a mutable object to an immutable object and vice-versa.
Can someone please provide me the correct syntax to do it?


Solution

  • I implemented it using the code:

    MyClass class>>classMethod: aValue
    
    anObject := self new value:aValue.
    anObject isImmutable: true.
    ^anObject.