Yet Another Case-statement Idea

Phil Weichert weichert at hal-pc.org
Sun Jul 5 19:04:24 UTC 1998


Chris,
  This best implemenation of the case statement is given in the article,
'Smalltalk Case Statements' by Paul Baumann in the July/August 1997 issue
of The Smalltalk Report.  Baumann's article illustrates the ease of
providing support for a case statement, gives examples of their use,  and
explains why the example case statement is appropiate.
  I am sending you the code directly.
Phil Weichert


Chris Reuter wrote:

> In working on my current and perpetual Squeak project, a roguelike
> game tentatively titled "The Infernal Tower"[1], I concluded that I
> could really use a case statement in order to properly handle
> keyboard input.  Even though there are at least two such mechanisms,
> one of which works without curly brackets, I decided to be
> trendy and invent my own.  I present it here for your perusal:
>
> I implemented the following method in Object:
>
> case: obj do: block
>       self = obj ifTrue: [block value].
>
> It is used as follows:
>
>    letter
>         case: $a
>         do: [
>             stream nextPut: 'Apple'.
>         ];
>
>         case: $b
>         do: [
>             stream nextPut: 'BeBox'.
>         ];
>
>         case: $c
>         do: [
>             stream nextPut: 'Commodore'.
>         ];
>
>         ...and so on...
>
>         case: $z
>         do: [
>             stream nextPut: 'Zilog'.
>         ].
>
> (I realize that this example could be implemented more easily using a
> Dictionary.  It's just for illustration.)
>
> Advantages:
>          -Looks like the switch/case statements we procedural
>           programmers know and love.
>          -It's very, very simple.
>          -It makes use of blocks and messages to do its thing, just
>           like the standard control structures.
>
> Disadvantages:
>
>          -Behaviour is subtly different from what C/Pascal programmers
>           would expect, in that all possibilities are tried rather
>           than just the one that matches the receiver.
>
>          -Because of this, it's also slower.  I can alleviate (sp?)
>           some of this by making the method return in the block, but
>           that's not always possible.
>
> In addition, I've implemented #case:case:do: (but not
> #case:case:case:do: or #case:case:case:case:do: yet) to simulate
> simple fall-through.  (#case:case:do evaluates the block if either of
> its arguments equals the receiver.
>
> I've also implemented #identityCase:do: which uses #== instead of #=
> to make its comparisons, but that message is somewhat awkward.
> Perhaps, I should make #case:do: use #== and implement a new method,
> say #caseEq:do: which uses #=.  I suspect it would also make it easier
> to optimize #case:do: at compile time, should somebody want to
> implement that.
>
> Now try THAT in C++!
>
>                                 --Chris
>
> [1] Currently 65% vapourware.





More information about the Squeak-dev mailing list