Smalltalk doesn't support the concept of enumerated types like in Java 5 and above. Instead, the Smalltalk way is to:
- create a class that represents the enumerated type
- add a class variable for each enumerated value (must start uppercase)
- add a class-side
initialize
method that creates an instance of the class for each enumerated value using basicNew
and assigns it the corresponding class variable - prevent creation of additional instances by overiding the class method
new
with "self error: 'new instances cannot be created'
" - add class-side getter method for each enumerated value that simply returns it
Here's an example ColorEnum
class.
Object subclass: #ColorEnum
instanceVariableNames: ''
classVariableNames: 'Blue Green Red'
poolDictionaries: ''
category: 'SomeCategory'
initialize
Red := self basicNew.
Green := self basicNew.
Blue := self basicNew
new
self error: 'new instances cannot be created'
red
^Red
green
^Green
blue
^Blue