[Newbies] enumerated types

Mark Volkmann mark at ociweb.com
Mon Oct 6 01:19:09 UTC 2008


Have I summarized this correctly?
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

---
Mark Volkmann




-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/beginners/attachments/20081005/e49b446d/attachment.htm


More information about the Beginners mailing list