[ENH] Nicer Case switches

Ned Konz ned at bike-nomad.com
Thu Jul 13 19:27:07 UTC 2000


I suspect no one is using Object>>caseOf: or Object>>caseOf:otherwise:
(actually, I just checked and found 2 places!), but if you want nicer
looking and more flexible case switches (including being able to defer
their evaluation), here's a change set.

It even comes with an SUnit test suite.

>From the class comment:

Case provides a more readable front end for the caseOf: and
caseOf:otherwise: methods in Object.
It makes case switches into first class objects that can be evaluated
later, with arguments
(if their receiver is a block with arguments) or without.

You can write code like:

#abc case
	on: [ #def ] do: [ 123 ];
	on: [ #abc ] do: [ 0 ];
	on: [ 'a' asSymbol ] do: [ 1 ];
	value

or make a deferred case switch that can be evaluated later:

myCase _ [ :a :b | a + b ] case
	on: [ 123 ] do: [ #oneHundredTwentyThree ];
	on: [ 42 ] do: [ #theAnswer ];
	otherwise: [ #IDontKnow ].

myCase valueWithArguments: #(121 2).
myCase valueWithArguments: #(40 2).

or just test blocks:

Case new
	on: [ a < 0 ] do: [ a _ a negated ];
	on: [ a = 0 ] do: [ a _ 123 ];
	otherwise: [ a _ 0 ];
	value.

Note that "Case new" produces the same thing as "true case".

-- 
Ned Konz
currently: Stanwood, WA
email:     ned at bike-nomad.com
homepage:  http://bike-nomad.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: CaseSwitch-nk.1.cs
Type: application/octet-stream
Size: 5114 bytes
Desc: not available
Url : http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20000713/e5314ee5/CaseSwitch-nk.1.obj
-------------- next part --------------
A non-text attachment was scrubbed...
Name: CaseSwitch-Testing-nk.1.cs
Type: application/octet-stream
Size: 5163 bytes
Desc: not available
Url : http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20000713/e5314ee5/CaseSwitch-Testing-nk.1.obj


More information about the Squeak-dev mailing list