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:
Here's an example 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 [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
On Mon, Oct 6, 2008 at 2:19 PM, Mark Volkmann <[hidden email]> 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. Gulik. -- http://people.squeakfoundation.org/person/mikevdg http://gulik.pbwiki.com/ _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
On Oct 5, 2008, at 8:38 PM, Michael van der Gulik wrote:
--- Mark Volkmann _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
On Mon, Oct 6, 2008 at 2:48 PM, Mark Volkmann <[hidden email]> wrote:
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. -- http://people.squeakfoundation.org/person/mikevdg http://gulik.pbwiki.com/ _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
In reply to this post by Mark Volkmann
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 > [hidden email] > http://lists.squeakfoundation.org/mailman/listinfo/beginners _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
Free forum by Nabble | Edit this page |