Switsch Statement like in java.

Matej Košík kosik at fiit.stuba.sk
Wed Sep 7 23:10:37 UTC 2005


Jason Rogers wrote:
> On 09/07/2005 09:41 AM, Matej Košík apparently said:
> 
>> If you find any natural example where cond:cond:cond:... where it 
>> would be advantageous to use it, I would be grateful if you let me 
>> know. Until know I have none.
> 
> 
> I realize there are better idioms for this (DoubleDispatch, Visitor, 
> etc.) however I used your COND package
> for some quick and dirty 
> processing of XML wherein I get the string value from the XML element 
> and convert it into an appropriate object.
> 
>    value
>        ^
>        [rawValue isNil or: [type = String class]] -> [self convert: 
> rawValue toAnInstanceOf: type]
>            cond:
>        [true] -> [self convert: rawValue withBlanksTrimmed 
> toAnInstanceOf: type]
> 
> 
>    convert: aRawValue toAnInstanceOf: aClass
>        ^
>        [aRawValue isNil] -> ['']
>            cond:
>        [aClass = String class] -> [aRawValue]
>            cond:
>        [aClass = Number class] -> [aRawValue asNumber]
>            cond:
>        [aClass = Boolean class] -> [aRawValue asBoolean]
>            cond:
>        [aClass = ByteArray class] -> [self 
> byteArrayFromBase64BinaryString: aRawValue]
>            cond:
>        [aRawValue isEmpty & (aClass = TimeStamp class)] -> ['']
>            cond:
>        [aClass = TimeStamp class] -> [TimeStamp readFrom: (ReadStream 
> on: aRawValue)]
> 
> I found myself using the "default" notion of the switch statement quite 
> a bit (as in the #value method):
> 
>    [true] -> ["some appropriate default"]

It was added to COND version 10. You can install it over the older version. Then you can look here

http://altair.dcs.elf.stuba.sk/wiki/Kosik/COND#TheOtherExample

and try something similar out. If you notice any problems, let me know.

> 
> As I said before there are better OO ways of doing all of this, however 
> I didn't need the extra complication.  I just wanted a quick and dirty 
> solution in this case, much like a scripting language offers, but I 
> wanted it to be more elegant than simple if/else statements.
> 

Both patterns are intersting. Thank you for pointers.

Regards
--
PS (Ivan): The relevant filed out source code of the new version as it was filed out can be found again here
http://altair.dcs.elf.stuba.sk/~kosik/tmp/COND/
-- 
Matej Košík
ICQ: 300133844
http://altair.dcs.elf.stuba.sk/wiki/Kosik/Main



More information about the Squeak-dev mailing list