[REBOL] Re: Unable to use Switch and Datatypes..
From: brian:hawley at: 7-May-2001 12:48
Scott Dunlop wrote:
>Is this a bug or a feature?
>The following code, which I would expect to generate 'A word is a word.',
>outputs 'A word is not always a word.', indicating that word! is not
>equivalent to word! for the purposes of switch. This also appears to be
>the case with the string! datatype. The interesting thing is that [word! >word!] returns
> switch/default type? 'aWord [
> word! [ print "a word is a word."]
> ] [
> print "A word is not always a word."
>Thanks in advance for any suggestions people have for working around this,
>aside from a long chain of Either statements..
It's a feature :)
The word word! is just a word until it is evaluated.
The block of cases passed to the switch function is
not itself evaluated - just the selected value. If
you are using literal values as keys this is not a
problem, even a speedup. For other values, try this:
switch/default type? 'aWord reduce [
word! [ print "a word is a word."]
print "A word is not always a word."
That evaluates the case block, changing words to the
corresponding values. Note that this creates a new
case block every time the expression is evaluated.
There are other hacks that can be done to evaluate
the block only once, such as using compose, that may
or may not be appropriate in your situation. Most of
the time it doesn't matter.
I hope this helps.