我有以下代码:
class Triangle
def initialize(@sides : Array(Int32))
@set = Set.new(@sides)
end
end
但是我得到一个编译器错误,它给了我:
Can't use Set(T) as the type of instance variable @set of Triangle, use a more specific type
我会想,因为 @sides
的类型是 Array(Int32)
,所以集合上会有类型推断 . 我已经阅读了the docs但没有在那里看到答案 .
2 回答
你必须设置集合的类型,如下所示:
实质上,实例变量的类型推断不够智能,无法确定泛型类型 . 我想它应该是可能的并且可能会在一段时间内实现,但是现在你必须明确地编写它 .