2017-04-03 365 views
1

试验语言我发现select在全局范围内定义,其优先级高于局部变量。什么是“select when”语法?

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以及它是如何使用?

在线搜索我看到在Enumerable,Hash,Channel和Array上定义的select ......但乍看起来似乎并不是这样。

感谢您的帮助!

回答

2

它类似于Go的选择:https://tour.golang.org/concurrency/5

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

+0

谢谢!这个功能有PR吗? –

+1

你去了:https://github.com/crystal-lang/crystal/pull/3130 –