【问题标题】:What is the "select when" syntax for?“选择何时”语法是什么?
【发布时间】:2017-08-28 23:49:36
【问题描述】:

实验语言发现select是在全局范围内定义的,其优先级高于局部变量。

def example(select)
  puts select
end
example 3
# Syntax error in eval:3: unexpected token: end (expecting when, else or end)

所以一步一步地尝试选择我得到了这个:

select 1 end
# Syntax error in eval:3: unexpected token: end (expecting when, else or end)

然后

select when 1 end
# Syntax error in eval:1: invalid select when expression: must be an assignment or call

然后

select when x = 1 end
# Syntax error in eval:1: invalid select when expression: must be an assignment or call

然后

select when x
# Syntax error in eval:1: unexpected token: EOF (expecting ',', ';' or '

我将跳过几个步骤,因为您应该知道我是如何回答我的问题的……

select when x;
else y
end
# Error in line 1: undefined local variable or method 'x_select_action'

最后

x_select_action = 4

select when x;
else y
end
# Error in line 3: undefined method 'x_select_action' (If you declared 'x_select_action' in a suffix if, declare it in a regular if for this to work. If the variable was declared in a macro it's not visible outside it)

所以语言中有这个关键字在局部变量优先级之前,我不知道它是做什么用的。但显然,当 x 作为 when 子句给出时,它会寻找 x_select_action。这个select 是做什么用的?它的用途是什么?

在线搜索我看到select 定义在 Enumerable、Hash、Channel 和 Array 上……但乍一看似乎不是这些。

感谢您的帮助!

【问题讨论】:

    标签: crystal-lang


    【解决方案1】:

    类似于 Go 的 select:https://tour.golang.org/concurrency/5

    但它仍然需要一些调整才能完成,这就是为什么还没有关于它的文档。

    【讨论】:

    • 谢谢!这个功能有 PR 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-18
    • 1970-01-01
    • 1970-01-01
    • 2013-01-14
    • 2021-08-12
    相关资源
    最近更新 更多