Why do I have to cast typeof to string with switch to make it work ?
This doesn't work:
typeof: type? get 'optional
switch typeof [
word! [
print "word"
]
string! [
print "string"
] 开发者_C百科
]
This works:
typeof: type? get 'optional
switch to-string typeof [
"word" [
print "word"
]
"string" [
print "string"
]
]
switch type?/word :optional [ word! [ print "word" ] string! [ print "string" ] ]
OR
switch type? :optional reduce [ word! [ print "word" ] string! [ print "string" ] ]
The reason is that the REBOL doesn't reduce ("evaluate") the cases in the switch statement. Without the /word
refinement, the type?
function returns a datatype!
, but the switch statement tries to match this against a word!
, and it fails.
I realize this might be confusing, so your best bet is either to convert the type to a string (as you did), or use one of the two idioms I've suggested. I prefer the first one, using type?/word
.
精彩评论