On 3 Jan 2019, at 03:17, Esteban Maringolo <emaringolo@gmail.com> wrote:
Hi Paul,
You can set the object to be "read only" (aka "immutable") and handle the exception.
E.g.
| u | u := User named: 'Paul'. u beReadOnlyObject. [ "your code modifying u's state" ] on: ModificationForbidden do: [ :ex | ex halt. "inspect state" u beWritableObject. ex resume ].
Probably there is a better way to do this with slots, using default ones or subclassing `InstanceVariableSlot` for that instance only and halting on the `write:to:` method of it.
I started an experiment to turn the read Only mechanism into a general object change notifier: https://github.com/MarcusDenker/ChangeDetector This allows the following style of use: testObserveObjectWithModification [ | observed observer | observed := 'test2'. observer := self. observed notifyOnChange: observer. "the observed object is now read only" self assert: (observed isReadOnlyObject). "but we can change it!" observed at: 1 put: $T. self assert: observed equals: 'Test2'. "and the method #objectChangeNotified was called, setting flag" self assert: flag equals: #yes. observed stopNotifyOnChange: observer. self deny: (observed isReadOnlyObject). missing: -> we need to model read only for all tracked object (we need to remember the old state and restore it) -> think about threads⦠is there a problem? Marcus