Rebol 开关和类型?
为什么我必须使用 switch 将 typeof 转换为字符串才能使其工作?
这不起作用:
typeof: type? get 'optional
switch typeof [
word! [
print "word"
]
string! [
print "string"
]
]
这起作用:
typeof: type? get 'optional
switch to-string typeof [
"word" [
print "word"
]
"string" [
print "string"
]
]
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"
]
]
This works:
typeof: type? get 'optional
switch to-string typeof [
"word" [
print "word"
]
"string" [
print "string"
]
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
OR
原因是 REBOL 不会减少(“评估”) switch 语句中的情况。如果没有
/word
细化,type?
函数会返回datatype!
,但 switch 语句会尝试将其与word 进行匹配!
,并且失败了。我意识到这可能会令人困惑,因此您最好的选择是将类型转换为字符串(就像您所做的那样),或者使用我建议的两个习惯用法之一。我更喜欢第一个,使用
type?/word
。OR
The reason is that the REBOL doesn't reduce ("evaluate") the cases in the switch statement. Without the
/word
refinement, thetype?
function returns adatatype!
, but the switch statement tries to match this against aword!
, 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
.