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
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
| x y zzz |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:In Squeak,
cue requestoris 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.