I've defined a Container
class. In the @values attribute, I need to store an array, or a 2D array, and elements in those arrays can be Int32 or Float64. If I initialize it like this:
class Container
def initialize(value)
@values = values
end
end
I get an error: @values : Type, is inferred from assignments to it across the whole program.
If I define it like this:
class Container
def initialize(value : Array)
@values = values
end
end
I get: can't use Array(T) as the type of instance variable @values of Container(T), use a more specific type
How can I make this class more flexible so I can do:
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]])
After doing some digging, there does seem to be an official way of doing this. However, it must be planned because using that syntax in the constructor gives me the following as of Crystal 0.20.1
If I am understanding correctly from your sample data, it seems like types will be homogeneous (i.e. arrays will always contain one specific type). If this is the case, you can simply overload the constructor. It's not a pretty solution, but perhaps it can tie you over.
Edit:
It seems you can use @faaq's solution without specifying the type thanks to the comment by @Sija. They also shared this sample code, which I think is much cleaner than overloading the constructor.