Rebol 开关和类型?

发布于 2024-08-07 07:04:39 字数 422 浏览 2 评论 0原文

为什么我必须使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰つ倒转 2024-08-14 07:04:39
switch type?/word :optional [
    word! [ print "word" ]
    string! [ print "string" ]
]

OR

switch type? :optional reduce [
    word! [ print "word" ]
    string! [ print "string" ] 
]

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文