Generating Smalltalk code
In the Public Interface Editor, from the
File menu select
Generate Default Scripts. The following window displays:
Because none of the methods exist yet, select Generate All. After VA Smalltalk generates the code for the methods, switch to the Script Editor to see the code.
First, notice the class definition for the part is as follows:
AbtAppBldrPart subclass: #MyTimer
instanceVariableNames: 'repeat length'
classVariableNames: ''
poolDictionaries: ''
Two instance variables have been added for you: repeat and length. The MyTimer class is a subclass of AbtAppBldrPart. AbtAppBldrPart provides all of the general properties and behaviors that a timer part needs.
As you gain experience building parts, you might find that you want to choose a different parent class when you create the part. You can inherit from a different class by typing the class name in the Inherits from field of the New Part window.
Select the Not categorized category. In the method list, you see length, length:, repeat, repeat:, start, and stop. Select length to see the code that was generated for you.
length
"Return the value of length."
^length
Before an instance variable has been set, its value is nil. A popular technique for initializing instance variables is lazy initialization, where the value is set the first time the instance variable is referenced. To use lazy initialization, add one line to the length method so it looks like the following:
length
"Return the value of length."
length == nil ifTrue: [self length: 0].
^length
The preceding initialization technique is a convenient way to prevent errors, and is generally easier than checking for nil values each time you reference the attribute. Follow the same steps for the repeat method, using false as the initial value, as follows:
repeat == nil ifTrue: self repeat: false].
The set methods do not need to be changed. To understand what they do, let's examine the code that was generated for the length: method:
length: anInteger
"Save the value of length."
length := anInteger.
self signalEvent: #length with: anInteger
The preceding method simply stores a new value into the variable, and tells VA Smalltalk that the value has been changed. Then VA Smalltalk can notify other parts of the new value.
Read-only attributes
For the timer's length and repeat attributes, you entered both get and set selector names in the Public Interface Editor. Usually, get and set selectors come in pairs.
The exception is when an attribute is read-only. If you do not want other parts to be able to set the value of an attribute, you should enter only the get selector. When the set selector is not defined, VA Smalltalk cannot set the attribute's value.
Last modified date: 05/14/2020