Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to uniquely identify a transcript window in Pharo/Squeak?

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?

like image 367
oscarkuo Avatar asked Apr 08 '11 08:04

oscarkuo


2 Answers

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

like image 110
Mariano Martinez Peck Avatar answered Oct 04 '22 09:10

Mariano Martinez Peck


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.

like image 31
Niall Ross Avatar answered Oct 04 '22 10:10

Niall Ross