(元组数组)的散列导致 compile/runtime 错误 "index out of bounds for tuple {(String, String -> Void)}"
Hash of (Array of Tuples) causing compile/runtime error "index out of bounds for tuple {(String, String -> Void)}"
我正在尝试创建一个包含元组的数组哈希。这导致了一个奇怪的问题,我怀疑是编译器错误,但我不是 100% 确定。
我使用的语法:
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
.
时不会出现此错误
这是 compiler/runtime 错误,还是我搞错了语法?
Crystal 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)
如果您计划在 Crystal 社区中采用您的图书馆,也请给 http://crystal-lang.org/docs/conventions/coding_style.html 阅读。
我正在尝试创建一个包含元组的数组哈希。这导致了一个奇怪的问题,我怀疑是编译器错误,但我不是 100% 确定。
我使用的语法:
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
.
这是 compiler/runtime 错误,还是我搞错了语法?
Crystal 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)
如果您计划在 Crystal 社区中采用您的图书馆,也请给 http://crystal-lang.org/docs/conventions/coding_style.html 阅读。