[Newbies] enumerated types

Michael van der Gulik mikevdg at gmail.com
Mon Oct 6 02:05:09 UTC 2008


On Mon, Oct 6, 2008 at 2:48 PM, Mark Volkmann <mark at 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.


-- 
http://people.squeakfoundation.org/person/mikevdg
http://gulik.pbwiki.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/beginners/attachments/20081006/ba3ec62f/attachment-0001.htm


More information about the Beginners mailing list