【发布时间】:2016-11-19 18:16:07
【问题描述】:
我在使用索引时发现了一些奇怪的行为。
#Defined in the class's initialize
@my_list = [] of Type
index = @my_list.index { |i| i.value == 2 } # => 0
@my_list[0] # => 2
@my_list[index] # => error
我得到错误:
没有重载匹配 'Array(Type)#[]' 类型为 (Int32 | Nil)
不知道为什么 index 不起作用,因为 index = 0。
编辑:
更多信息。如果我这样做:
if index == nil
#Do something
#error => undefined method '>=' for Nil (compile-time type is (Int32 | Nil))
elsif index >= 0
#Do something else
end
我明白了。它可能为零,但由于我已经在检查它是否为零,所以这里不应该有问题。我认为之前的代码 sn-p 遇到了同样的问题。
【问题讨论】:
标签: crystal-lang