"select when" 语法有什么用?
What is the "select when" syntax for?
试验语言我发现 select
是在全局范围内定义的,它的优先级高于局部变量。
def example(select)
puts select
end
example 3
# Syntax error in eval:3: unexpected token: end (expecting when, else or end)
所以一步一步地尝试 select 我得到了这个:
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...但是乍一看好像不是这些
感谢您的帮助!
它类似于 Go 的 select:https://tour.golang.org/concurrency/5
但它仍然需要一些调整才能完成,这就是为什么还没有关于它的文档的原因。
试验语言我发现 select
是在全局范围内定义的,它的优先级高于局部变量。
def example(select)
puts select
end
example 3
# Syntax error in eval:3: unexpected token: end (expecting when, else or end)
所以一步一步地尝试 select 我得到了这个:
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...但是乍一看好像不是这些
感谢您的帮助!
它类似于 Go 的 select:https://tour.golang.org/concurrency/5
但它仍然需要一些调整才能完成,这就是为什么还没有关于它的文档的原因。