Object>>caseOf: (was Re: [Q] Enum style Object?)

Ian Piumarta ian.piumarta at inria.fr
Wed Feb 19 13:56:47 UTC 2003


On Wed, 19 Feb 2003 tblanchard at mac.com wrote:

> 
> On Wednesday, February 19, 2003, at 11:58  AM, Marco Paga wrote:
> 
> > Hi all.
> > Using switch/case/many if's is in OO languages the sign that something 
> > is going very wrong. The Object>>caseOf: /otherwise: should get banned 
> > from the image.
> 
> You know, ordinarily I agree with you - but its a terribly handy thing 
> when parsing input - read a character and then caseOf: do the right 
> thing with it.

Take a look at how the Scanner works.  It does "the right thing".  An
Array of 256 symbols indexed by the initial character in each token to
provide a selector for #perform: that invokes the method responsible for
parsing the rest of the token.  (The selectors in question all start with
'x'.)

OTOH...

> So I think banning it from the image is a bit strong.

...there are times when the input is not neatly represented by something
you can turn into a cardinal within a smallish range, from which to build
a dispatch table.  #caseOf: solves this one way, although another way
would be to replace Array in the above solution with [Identity]Dictionary.

Ian



More information about the Squeak-dev mailing list