[Pharo-users] How to intercept instvar write - MetaLinks + anonymous subclass problem

Petr Fischer petr.fischer at me.com
Sat Feb 2 10:26:13 EST 2019

> > is possible to intercept (before writeú all instance variable writes on every existing objects? 
> > 
> > Ok, I learned more about MetaLinks and it's perfectly possible to intercept instance variable writes via. installing MetaLink into object instance (cool) - then I can intercept instvar write, but one problem immediately occurs:
> > when installing MetaLink into object instance, an original object class is exchanged to anonymous subclass of the original class - and because typical #= method is implemented like:
> > 
> > =
> > self == anObject
> > ifTrue: [ ^ true ].
> > self class = anObject class
> > ifFalse: [ ^ false ].
> > ...
> > 
> > then most of #= comparisons in application logic is broken due to "self class = anObject class" part.
> > 
> > Is there any way to solve this problem?
> Hi, 
> the anonymous class thing only happen when you install an object-centric
> metalink, i.e. on only one specific object. I also wanted to hide the
> anonymous class, or maybe to only make it visible for environment tools.
> I need to talk about that with Marcus. 
> Why not installing your metalink on the classes of your objects, so that
> all instances are affected but in that case there is no anonymous class
> migration? 
> If that is not possible, in your case maybe a (temporary) hack would be,
> in your case, to add a test in the #class method in Object (or wherever
> it is defined): 
> class 
>     |myClass| 
>     myClass := self class. 
>     ^myClass isAnonymous ifTrue:[myClass superclass] ifFalse:[myClass] 
> But i did not test that and i have no idea of the overall impact. 
> Steven. 

Thanks for answer! I want to detect instvar writes only for objects (instances), that was previously fetched from external "datastore" (DB like), so I can persist changed objects only back to the datastore at the end of transaction. These objects can be any existing class (Pharo classes, some 3rd party libs classes, everything), so 1) injecting a class is overkill (affects all objects in the image) 2) impossible to rewrite #class method for every existing class.

Injecting object-centric metalinks via anonymous subclass breaks up many things, when I want to treat injected object like any othres (IMHO).

If you will be able to avoid this issue, that would be extra cool :-]

Note: for now, best for my "instvar writes detection" would be probably hack Object>>#attemptToAssign:withIndex: or ProtoObject>>#modificationForbiddenFor:index:value: and make my "datastore fetched objects" read only (via Object>>#setIsReadOnlyObject:).


More information about the Pharo-users mailing list