I'm new to smalltalk and like every other programming languages I've learnt, I tried to do a simple "hello world" program. I've learnt that in Pharo/Squeak what you do is to open a transcript window, typed following code into the workspace window and then pressed 'Alt-d' to run it:
Transcript show: 'hello world'; cr.
As far as I understand, the line means send the "show" message with argument "hello world" to the Transcript object and as expected, my transcript window now shows:
hello world
so far so good, however, when I opened a second transcript window and ran the code again, I found that both transcript windows now have:
hello world
As if both transcript windows are identified by the identifier "Transcript". This is what got me confused because I would have thought that transcript windows must be "instances" of the transcript window class. Therefore, shouldn't there be a way to uniquely identify them?
This is a complicated and excellent question. Smalltalk has a large dictionary where all globals are stored. Globals are usually classes, but there can be any object there. It is just a dictionary (key/value). This large dictionary I am talking about is called Smalltalk. Write "Smalltalk inspect" and you will see it. It is the unique instance of SmalltalkImage class.
So...when in your code you type MyClass this is because in "Smalltalk at:#MyClass" in the value, you have the class. But you can also do: Smalltalk at:#mariano put: 'mariano'. Then you can write "mariano" everywhere, and you will get the string 'mariano'.
Transcript is like that. In (Smalltalk at:#Transcript) it is kept the unique instance of Transcript. Which, indeed, may not be instance of a Transcript class but another one. In pharo, it is instance of ThreadSafeTranscript. Check this:
ThreadSafeTranscript instanceCount -> 1 Transcript open. Transcript open. Transcript open.
And you will always have one. Because Transcript itself is the MODEL. Not the view. The view (the windows that are opened when you open a Transcript) are instances of PluggableTextMorph, whose model, is the unique instance of Transcript. Check it:
PluggableTextMorph instanceCount ->> 11 Transcript open. Transcript open. Transcript open. PluggableTextMorph instanceCount ->> 14
Fore more details, debug "Transcript open".
Cheers
Transcript is a global variable holding the instance. Inspect it to see its class and thus see how to open another window, to which you would hold a local reference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With