Hello world example with Environments 2
Last updated at 11:05 am UTC on 16 May 2017
The addition to the previous version is how to open workspaces with the new environments
spanish beCurrentDuring: [Workspace open label: 'Workspace - environment spanish'].
english beCurrentDuring: [Workspace open label: 'Workspace - environment english'].
The code for the example above
| createHelloClass createHelloMethod english spanish |
"Straight but verbose code to create a Hello class and compile a say method.
There's one trick: Environment current, otherwise Compiler would evaluate in nil class environment, not good"
createHelloClass := [Object subclass: #Hello
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'Test'].
createHelloMethod := [:greeting |
| methodSource sourceCode |
methodSource := 'say Transcript cr; show: ' , greeting printString.
sourceCode := 'Hello class compile: ' , methodSource printString , ' classified: ' , 'test' printString.
Compiler evaluate: sourceCode environment: Environment current].
"Create the english and spanish environments"
english := Smalltalk globals.
spanish := Environment withName: 'Spanish'.
spanish importSelf.
spanish from: english import: #Transcript.
"Create the class and compile the method in each environment:"
[createHelloClass value.
createHelloMethod value: 'Hello world'] on: CurrentEnvironment do: [:exc | exc resume: english].
[createHelloClass value.
createHelloMethod value: 'Buenos dias'] on: CurrentEnvironment do: [:exc | exc resume: spanish].
spanish beCurrentDuring: [Workspace open label: 'Workspace - environment spanish'].
english beCurrentDuring: [Workspace open label: 'Workspace - environment english'].
"Execute 'Hello say' in the two workspaces which has been opened".
"Greet from"
Compiler evaluate: 'Hello say' environment: english.
Compiler evaluate: 'Hello say' environment: spanish.
Compiler evaluate: 'Hello say' environment: english.
"Cleanup"
[Compiler evaluate: 'Hello removeFromSystem' environment: Environment current] on: CurrentEnvironment do: [:exc | exc resume: english].
[Compiler evaluate: 'Hello removeFromSystem' environment: Environment current] on: CurrentEnvironment do: [:exc | exc resume: spanish].