导致编译/运行时错误的“元组数组”的哈希值“索引超出元组{(String,String - > Void)}}
问题描述:
我想使用元组进行哈希阵列。这造成了一个奇怪的问题,我怀疑是一个编译器错误,但我不是100%确定的。导致编译/运行时错误的“元组数组”的哈希值“索引超出元组{(String,String - > Void)}}
我使用的语法:
class SomeClass
@@weird_object = Hash(String, Array(Tuple(String, String ->))).new {|h, k| h[k] = [] of Tuple(String, String ->) }
#...
def self.some_method
@@weird_object.each do |tuple|
pp tuple[0]
pp tuple[1] #=> This causes compile error "index out of bounds for tuple {(String, String -> Void)}"
end
@@weird_object.each do |the_sting, callback|
#this causes a "Nil trace:" for the callback parameter
end
#...
end
看起来这已经成为一个(String, String -> void)
对象的元组,而不是String, String -> void
。当我运行crystal spec
时出现此错误,但在运行crystal build ./src/main.cr
时没有。
这是编译器/运行时错误,还是我搞砸了语法?
水晶0.8.0 [e363b63(星期六9月19日12时00分17秒UTC 2015年)
答
您的实际代码有:
Hash(String, Array({String, String -> })).new
这元组一样
{(String, String ->)}
所以你需要parens消除歧义
Hash(String, Array({String, (String ->)})).new
您的实际代码在出现无处不在的Void时也存在问题,我不知道为什么,但几个演员阵营可以解决它。下面是完整的差异,使其工作
diff --git a/src/untangle/aggregator.cr b/src/untangle/aggregator.cr
index 8c49681..7553eb2 100644
--- a/src/untangle/aggregator.cr
+++ b/src/untangle/aggregator.cr
@@ -3,7 +3,7 @@ class Aggregator
@@subscribers_all = Array(String, String ->).new
@@responders = Hash(String, (String ->)).new
#@@reroutes = {} of String => Proc(String)
- @@reroutes = Hash(String, Array({String, String -> })).new {|h, k| h[k] = Array({String, String -> }).new }
+ @@reroutes = Hash(String, Array({String, (String ->)})).new {|h, k| h[k] = Array({String, (String ->)}).new }
def self.subscribe (message_type, &callback : String ->)
@@subscribers[message_type] << callback
@@ -35,11 +35,11 @@ class Aggregator
spawn do
message_type = ch.receive
data = ch.receive
- callback.call(message_type, data)
+ callback.call(message_type, data as String)
end
ch.send(message_type)
- ch.send(data)
+ ch.send(data as String)
end
@@subscribers[message_type].each do |callback|
@@ -48,14 +48,14 @@ class Aggregator
spawn do
callback.call(ch.receive)
end
- ch.send(data)
+ ch.send(data as String)
end
@@reroutes[message_type].each do |tuple| #|to_type, callback|
# puts "!!!!!!!!!!!!!!!!!!!"
# pp tuple
# puts "!!!!!!!!!!!!!!!!!!!"
- Aggregator.publish(tuple[0], tuple[1].call(data))
+ Aggregator.publish(tuple[0], tuple[1].call(data as String))
end
end
def self.request(message_type, arguments)
请也给http://crystal-lang.org/docs/conventions/coding_style.html读,如果你在水晶社区规划采用库的。
+0
我尝试了两种语法,但是,它需要两个变体中的括号。谢谢!而且,是的,现在的代码很混乱,因为我正在尝试各种事情来解决这个特定的问题。 – Automatico
你能重新检查你的例子吗?因为对于我来说,在第一行中修改变量名中的拼写错误,我没有得到你描述的编译错误。 – waj
我在SO上重命名时输入了错误。我仍然得到错误。 – Automatico
但是,我在课堂范围内声明这可能是问题。更新问题,坚持下去。 – Automatico