【问题标题】:Rebol switch and type?Rebol 开关和类型?
【发布时间】:2009-10-11 16:13:13
【问题描述】:

为什么我必须使用 switch 将 typeof 转换为 string 才能使其工作?

这不起作用:

  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"
    ]           
  ]

【问题讨论】:

  • REBOL 是一种简单的语言,但与 Carl Sassenrath 的断言相反,它不一定是一种简单的语言。 :) 简单!= 简单。
  • 那么让我们说它是一种优雅的语言:)

标签: rebol


【解决方案1】:
切换类型?/word :optional [
    单词! [打印“单词”]
    细绳! [打印“字符串”]
]

开关类型? :可选减少[
    单词! [打印“单词”]
    细绳! [打印“字符串”]
]

原因是 REBOL 没有减少(“评估”)switch 语句中的情况。如果没有 /word 细化,type? 函数会返回 datatype!,但 switch 语句会尝试将其与 word! 进行匹配,但它失败了。

我意识到这可能会令人困惑,因此最好的办法是将类型转换为字符串(就像您所做的那样),或者使用我建议的两个习语之一。我更喜欢第一个,使用type?/word

【讨论】:

  • 非常感谢,您肯定是 Rebol 大师:我不知道 /word 确实存在 :)
  • 谢谢!我也不知道/word,直到一位有用的评论者在我的博客上向我指出。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-02
  • 1970-01-01
  • 2021-10-24
相关资源
最近更新 更多