When is the class initialization dispatched?
I am wondering when class side initialization messages are sent in Smalltalk (especially Pharo and Squeak). Is there a specific order? At the very least, is it safe to assume that all the other classes packaged with it are already loaded and compiled, or the system is eagerly initializing (send the initialization before you finish loading and compiling other classes)?
source to share
The initialize
class class is never posted by the system. During development, you do it manually (which is why many of these methods have a comment "self initialize"
.
When you export a class code to a changeset, the exporter sends a message initialize
at the very end, so it runs when the class is loaded on another system.
This behavior mimics Monticello. When the class is loaded for the first time or when the method code changes, initialize
it is executed. This is because conceptually MC builds a changeset on the fly containing the difference between what is already in the image and what the download package contains. If this diff includes a initialize
class-side method , it will be executed when that version of the package is loaded.
source to share
As you asked about downloading and compiling, I assume what you mean when downloading the code ...
When loading a package or changeset, calls to the #initialize class are called after all the code has been installed (1) . While you cannot count on a specific ordering, you can assume that all the classes and methods from this package are loaded.
As Bert pointed out, if you didn't load but did #nitialize on the class side, you had to send the message yourself.
source to share
One way to find out for sure is to test it yourself. Smalltalk systems make this a little more accessible than many other systems. Just define your own MyTestClass
and then add your own class message (this is important) initialize
so you can discover when it fires, how often it fires, etc.
initialize
Transcript show: 'i have been INITIALIZED!!! Muwahahahah!!!'
Verify that it works by opening Transcript
and running
MyTestClass initialize
from a Workspace
. Now you can play with serve and return, loading Monticello, no matter when it is running.
source to share