[squeak-dev] Setting class instance variable nil...that's interesting

Thiede, Christoph Christoph.Thiede at student.hpi.uni-potsdam.de
Sat Sep 12 14:51:04 UTC 2020


Hi Timothy,


Please ignore if I chose the wrong level of explanation, this is always hard for me. :-)


All code executed in an inspector is executed against the inspected object as a receiver. That means that you can do absolutely everything with the object (including its class' state) you could do from a method compiled on its class as well.


> TemplateTitleWords := nil


This is of course valid syntax when executing the assignment against a class that contains a class variable (!= class instance variable!) named like this.


> WikitextTemplates TemplateTitleWords :=  nil.  does not (as expected)

This is no valid Smalltalk syntax (and has never been). Variables (except globals) can only be accessed when compiling against the defining class. This guarantees real encapsulation of an object's state, which is great to preserve modularity. If you want to access state from a foreign class, you have to define an accessor message by implementing an accessor method returning the state variable, because only messages can be used to communicate with foreign objects.

In your example, navigate to the class side of WikitextTemplates and write a method #templateTitleWords: that directly assigns the parameter to the TemplateTitleWords variable.

WikitextTemplates >> templateTitleWords: aCollection
    TemplateTitleWords := aCollection.

Be cautious not to expose too many implementation details of a class by defining accessors.

If you need a meta way to do this, have a look at #classPool and its senders. :-)

Best,
Christoph

________________________________
Von: Squeak-dev <squeak-dev-bounces at lists.squeakfoundation.org> im Auftrag von gettimothy via Squeak-dev <squeak-dev at lists.squeakfoundation.org>
Gesendet: Samstag, 12. September 2020 16:18:02
An: squeak-dev
Betreff: [squeak-dev] Setting class instance variable nil...that's interesting

Ignore if spam...


It is interesting that I can reset a class instance variable inside one of the methods of a class with a doit.
However, I cannot do it from a Workspace .


TemplateTitleWords := nil  from within the method area works (surprising, but very handy).
WikitextTemplates TemplateTitleWords :=  nil.  does not (as expected)

I expect the latter, I am surprised by the former.

Don't get me wrong, it is a handy feature , but if it is a bug, here is a heads up.

cheers,

tty

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20200912/31e1c0ce/attachment.html>


More information about the Squeak-dev mailing list