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
On Mon, Oct 6, 2008 at 2:19 PM, Mark Volkmann mark@ociweb.com wrote:
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:
<snip>
What are you using enums for? I've never found I've needed them, often because the need for them can be refactored away and you end up with cleaner code.
Often I just use symbols (i.e. #red, #blue, #green). These are generic descriptive names for thingies.
Gulik.
On Oct 5, 2008, at 8:38 PM, Michael van der Gulik wrote:
On Mon, Oct 6, 2008 at 2:19 PM, Mark Volkmann mark@ociweb.com wrote: 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:
<snip>
What are you using enums for? I've never found I've needed them, often because the need for them can be refactored away and you end up with cleaner code.
Often I just use symbols (i.e. #red, #blue, #green). These are generic descriptive names for thingies.
I haven't used them yet in Smalltalk, but I'm thinking I'll have a need to verify that a value passed to a method is a member of a confined set. Maybe I should just test a symbol passed as an argument to a method to see if it's in an array of allowed symbols.
--- Mark Volkmann
On Mon, Oct 6, 2008 at 2:48 PM, Mark Volkmann mark@ociweb.com wrote:
On Oct 5, 2008, at 8:38 PM, Michael van der Gulik wrote:
What are you using enums for? I've never found I've needed them, often because the need for them can be refactored away and you end up with cleaner code.
Often I just use symbols (i.e. #red, #blue, #green). These are generic descriptive names for thingies.
I haven't used them yet in Smalltalk, but I'm thinking I'll have a need to verify that a value passed to a method is a member of a confined set. Maybe I should just test a symbol passed as an argument to a method to see if it's in an array of allowed symbols.
Well, there are simpler ways of doing that such as:
isMemberOfConfinedSet: anArg (ValidValues includes: anArg) ifFalse: [self error: 'foo']. ...
Where ValidValues is a class variable and a Collection of valid values.
Alternatively, a more Smalltalkish way that I don't like:
isMemberOfConfinedSet: anArg (anArg isSomething) ifFalse: [self error: 'foo'].
Where >>isSomething is implemented to return true on all objects that could be a member. This method I find rather intrusive, especially if implemented on core classes like Object and String, but does run very fast because a simple method that just returns true has special optimisations in the VM.
Gulik.
On Sun, 2008-10-05 at 20:19 -0500, Mark Volkmann wrote:
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:
1. create a class that represents the enumerated type 2. add a class variable for each enumerated value (must start uppercase)
I wouldn't create class variables for the enumerated stuff. You can create a dictionary that holds the instances. It is more flexible to.
cache ^ cache ifNil: [ cache := Dictionary new ]
red ^ self cache at: #red ifAbsentPut: [ self new ... .... ]
blue ^ self cache at: #blue ifAbsentPut: [ self new ... .... ]
1. 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
see above
1. prevent creation of additional instances by overiding the class method new with "self error: 'new instances cannot be created'"
see above
1. add class-side getter method for each enumerated value that simply returns it
see above
I can see you have singletons in mind :) This approach is good if you need constant objects that should provide a richer protocol than just identity.
Norbert
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
Beginners mailing list Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners
beginners@lists.squeakfoundation.org