Setting class instance variable nil...that's interesting

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Setting class instance variable nil...that's interesting

Squeak - Dev mailing list
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



Reply | Threaded
Open this post in threaded view
|

Re: Setting class instance variable nil...that's interesting

Eliot Miranda-2
Hi tty,

On Sep 12, 2020, at 7:18 AM, gettimothy via Squeak-dev <[hidden email]> wrote:


Ignore if spam...

It’s not 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 .

The creasing is that doits always happen in the context of a specific receiver and different contexts have different receivers.  Trace Compiler class>>#evaluate: down and you’ll see where the receiver is supplied before the Compiler instance is created.

In a Workspace the receiver is nil.
In an Inspector the receiver is the object being inspected 
In an explorer the receiver is the currently selected object (IIRC)
In a browser the receiver is the currently selected class (or nil if none selected, or the current method’s class in a method list browser)

And within a doit (naturally since a doit is simply an anonymous method) all the inst cars if the receiver are in scope.  Since nil has no inst vars there are no inst vars in scope in 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




Reply | Threaded
Open this post in threaded view
|

Re: Setting class instance variable nil...that's interesting

Christoph Thiede
In reply to this post by Squeak - Dev mailing list

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 <[hidden email]> im Auftrag von gettimothy via Squeak-dev <[hidden email]>
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



Carpe Squeak!
Reply | Threaded
Open this post in threaded view
|

Re: Setting class instance variable nil...that's interesting

Squeak - Dev mailing list
In reply to this post by Eliot Miranda-2
Christoph and Eliot,

Thank you both.






---- On Sat, 12 Sep 2020 10:40:20 -0400 Eliot Miranda <[hidden email]> wrote ----

Hi tty,

On Sep 12, 2020, at 7:18 AM, gettimothy via Squeak-dev <[hidden email]> wrote:


Ignore if spam...

It’s not 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 .

The creasing is that doits always happen in the context of a specific receiver and different contexts have different receivers.  Trace Compiler class>>#evaluate: down and you’ll see where the receiver is supplied before the Compiler instance is created.

In a Workspace the receiver is nil.
In an Inspector the receiver is the object being inspected 
In an explorer the receiver is the currently selected object (IIRC)
In a browser the receiver is the currently selected class (or nil if none selected, or the current method’s class in a method list browser)

And within a doit (naturally since a doit is simply an anonymous method) all the inst cars if the receiver are in scope.  Since nil has no inst vars there are no inst vars in scope in 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