Unable to use Switch and Datatypes..
[1/5] from: scott::dunlop::home::com at: 7-May-2001 9:07
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 true.
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..
--Scott.
[2/5] from: jelinem1:nationwide at: 7-May-2001 11:12
Verified the problem here - that behavior doesn't seem right. Some simple
testing:
>> some-type: word!
== word!
>> switch/default some-type [word! [print "ok"]][print "not ok"]
ok
>> some-type: type? 'aword
== word!
>> switch/default some-type [word! [print "ok"]][print "not ok"]
not ok
Seems like the literal word! doesn't match to the return of 'type?. To
support this, see below. Note that the return of 'type? is forced into the
case
section of the switch, and it works:
>> case1: [x [print "ok"]]
== [x [print "ok"]]
>> change case1 type? 'aword
== [[print "ok"]]
>> print mold case1
[word! [print "ok"]]
>> switch/default type? 'aword case1 [print "not ok"]
ok
Looks like a bug to me.
As a work-around, if you don't want to violate your case-statements like
that, you could 'form or 'mold the results of 'type? and use strings for
the comparisons (yeah it's kinda strange, but 'mold returns a string on
datatypes).
- Michael Jelinek
Scott Dunlop <[scott--dunlop--home--com]>@rebol.com on 05/07/2001 09:07:30 AM
From: Scott Dunlop <[scott--dunlop--home--com]>@rebol.com on 05/07/2001 09:07 AM
Please respond to [rebol-list--rebol--com]
Sent by: [rebol-bounce--rebol--com]
To: [rebol-list--rebol--com]
cc:
Subject: [REBOL] Unable to use Switch and Datatypes..
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 true.
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..
--Scott.
[3/5] from: larry:ecotope at: 7-May-2001 10:26
Hi Scott
Just a quick tip. This works:
switch/default type?/word 'aWord [
word! [ print "a word is a word."]
] [
print "A word is not always a word."
]
You need to have word! be a word in the switch list, not a datatype.
-Larry
[4/5] 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.',
<<quoted lines omitted: 8>>
>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.
Brian Hawley
[5/5] from: brian:hawley at: 7-May-2001 13:18
Larry Palmiter wrote:
>Hi Scott
>Just a quick tip. This works:
<<quoted lines omitted: 5>>
>You need to have word! be a word in the switch list, not a datatype.
>-Larry
Well, that's much cleaner than my solution :)
I never cease to be amazed at the clever little improvements
that have been added to REBOL over time. Sometimes it seems
like there isn't a day that goes by without one of the work-
arounds that I've found over the years becoming obsolete.
Bless Feedback, and this list :)
Brian Hawley
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted