Switsch Statement like in java.
Jason Rogers
jacaetevha at fast-mail.org
Wed Sep 7 14:38:28 UTC 2005
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"]
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.
--
Jason Rogers
"I am crucified with Christ: nevertheless I live; yet not I,
but Christ liveth in me: and the life which I now live in
the flesh I live by the faith of the Son of God, who loved
me, and gave himself for me."
Galatians 2:20
More information about the Squeak-dev
mailing list
|