使 Array(T) 类型的 Class 成员属性接受 T 的二维数组

Make Class member attribute of type Array(T) accept 2D arrays of T

我已经定义了一个 Container class。在@values 属性中,我需要存储一个数组,或者一个二维数组,这些数组中的元素可以是 Int32 或 Float64。如果我这样初始化它:

class Container

  def initialize(value) 
    @values = values
  end

end

我得到一个错误:@values : Type, is inferred from assignments to it across the whole program.

如果我这样定义它:

class Container

  def initialize(value : Array) 
    @values = values
  end

end

我得到:can't use Array(T) as the type of instance variable @values of Container(T), use a more specific type

我怎样才能使这个 class 更灵活,这样我就可以做到:

Container.new([1,2,3])
Container.new([1.0, 3.0, 4.0])
Container.new([[1, 2], [4,3,2],[1]])
Container.new([[1.0, 4.5], [2.2, 0.0]])

经过一番挖掘,似乎确实存在 official way of doing this。但是,必须对其进行计划,因为在构造函数中使用该语法为我提供了以下 Crystal 0.20.1

def initialize(value : Array(Array | Int32 | Float64))
    @values = value
end

Error in line 3: can't use Array(T) in unions yet, use a more specific type

如果我从你的示例数据中理解正确,那么类型似乎是同质的(即数组将始终包含一种特定类型)。如果是这种情况,您可以简单地重载构造函数。这不是一个很好的解决方案,但也许可以解决问题。

class Container

  def initialize(value : Array(Array)) 
    @values = value
    calculate
  end

  def initialize(value : Array(Int32))
    @values = value
    calculate
  end

  def initialize(value : Array(Array(Int32)))
    @values = value
    calculate
  end

  def initialize(value : Array(Array(Float64)))
    @values = value
    calculate
  end

  def initialize(value : Array(Float64))
    @values = value
    calculate
  end

  def calculate
    # do stuff here
  end

end

Container.new([1,2,3])
Container.new([1.0, 3.0, 4.0])
Container.new([[1, 2], [4,3,2],[1]])
Container.new([[1.0, 4.5], [2.2, 0.0]])

编辑:

由于@Sija 的评论,您似乎可以在不指定类型的情况下使用@faaq 的解决方案。他们还分享了 this sample code,我认为这比重载构造函数要干净得多。

为什么不使用泛型?

class Container(Type)

  def initialize(@values : Type) 
    pp @values
    pp typeof(@values)
  end

end

value = [1,2,3]
Container(typeof(value)).new(value)
value = [1.0, 3.0, 4.0]
Container(typeof(value)).new(value)
value = [[1, 2], [4,3,2],[1]]
Container(typeof(value)).new(value)
value = [[1.0, 4.5], [2.2, 0.0]]
Container(typeof(value)).new(value)

输出为:

@values # => [1, 2, 3]
typeof(@values) # => Array(Int32)
@values # => [1.0, 3.0, 4.0]
typeof(@values) # => Array(Float64)
@values # => [[1, 2], [4, 3, 2], [1]]
typeof(@values) # => Array(Array(Int32))
@values # => [[1.0, 4.5], [2.2, 0.0]]
typeof(@values) # => Array(Array(Float64))