Simpler Scripts
Last updated at 4:39 pm UTC on 15 October 2019
CAVEAT. This example just prints something in the Transcript window, make sure it is open.
Simple scripts in Smalltalk are usually written as:
|btn|
btn := SimpleButtonMorph new.
btn label: 'Tell me hello'.
btn target: [Transcript show: 'hello world !'; cr].
btn actionSelector: #value.
btn openInWorld.
The btn variable as defined here is a so called Block variable.
It is personal opinion of the new user NMI that this is not good for beginners. There are indeed a few drawbacks.
- The `btn` variable is not easily accessible from the Workspace after the snippet is run.
- The user is forced to run the snippet all at once. This is bad, most of the fun stuff can be observed when running one line after another. Expecially if `openInWorld` happens early.
- The block script messes up very often the syntax highlighter and all the block becomes red. (this is just a bug)
NMI proposes this simpler style of scripting for beginners, and code snippets in general:
btn := SimpleButtonMorph new.
btn openInWorld.
btn label: 'Tell me hello'.
btn target: [Transcript show: 'hello world !'; cr].
btn actionSelector: #value.
The btn variable as defined here is a so called Workspace variable .
It is reccomended that the beginner runs the previous script one line at a time and see what happens.
Exercise-1. Change the script to produce 'Hello Saturn!'. You are required to modify and run ONLY ONE of the previous code lines to achieve the result.
Exercise-2. Change the button label to whatever you wish. This time you are required not to change the old code but to add and run a single NEW line of code in the Workspace.
Exercise-3. Change the button color. [Suggestion. send the messages `color` and `color:` to the `btn` object. Explore the Color class.]
tagBeginner