How to define a variable in Strongtalk (Smalltalk) and print it

57 views Asked by At

Does anyone know how to define a variable in Strongtalk-2.0 (Smalltalk) and print it on screen? In "normal" Smalltalk it's like

x := 2. Transcript show: x.

but in Strongtalk ( x < Integer > := ... ) ? - or is it only possible inside functions/blocks or classes?

Thanks, Iggi

1

There are 1 answers

0
aka.nice On

The Smalltalk compiler that is used to evaluate the code must have a knowledge of the context into which it must be evaluated: that includes the receiver class, so that the compiler can bind the variable names to some slot or shared variable.

There are several kind of variables

  • instance variables of receiver class which give access to a slot of object contents in memory
  • local variable which give access to a slot in the execution stack. That is either the method arguments, or the local variables declared between vertical bars | x y zzz |
  • shared variables that give access to class variables, pool variables, global variables, or any other shared (static) variables, thru an Association (key=Variable name, value=Variable value) which is shared in some Dictionary, or some equivalent class depending on dialect implementation

The Compiler has a list of variables accessible determined by the scope (local variables, then instance variables, then class variables, pool variables, class variables of superclss, etc...). Generally this scope is resolved in a parsing phase, so as to give ability to propose interactive correction hint when the variable name cannot be resolved (maybe correct atypo, or add an instance or local variable,...). In Squeak for example, that happens in the method Parser>>correctVariable:interval:

In order to ease the interaction, a convenience has been added in a lot of dialects (the Squeak/Pharo/Cuis family, Visualworks, ...): some shared variables are added as local to the Workspace or any other object representing the scope of user interface.

That's convenient, because those shared variables persist across several evaluations: it enables the user to split the evaluation of some code in a Workspace into several steps, as we would do in a Debugger.

The declaration of those variables can be either automated, or confirmed by the user thru a Dialog interaction, at dialect discretion. In Squeak, that happens in Parser>>correctVariable:interval: thru this code:

"First check to see if the requestor knows anything about the variable"
(binding := cue requestor ifNotNil: [:object | object bindingOf: proposedVariable])
    ifNotNil: [^encoder global: binding name: proposedVariable].

In Squeak, cue requestor is set to be the Workspace into which the expression is evaluated, Workspace>>bindingOf: will do the job of handling variables local to the context...

Of course implementation will vary across dialects.

Unfortunately, it seems that such convenience has not been added to Strongtalk natively. So you must resort to what the dialect proposes, probably just declare that x is a local variable by inserting | x | at the beginning of evaluation. But the value of x won't persist across next evaluation (as a local variable, x is a new slot in each evaluation, in the context stack).

The good news is that it is Smalltalk: you can change the Behavior if you will, I gave you some hints from another dialect, but it will be more or less equivalent.