Padre Diabledasie
Padre Diabledasie

Reputation: 3

how to add any hash to empty array of hashes?

I just learn Crystal, but stuck on an array of mix type hashes.

Suppose there is an array of hashes:

array = [{"a" => "Aa", "b" => 66, "c" => Time.now}]

I can easily add another element:

array << {"c" => "Bb", "d" => 2, "f" => 1.year.from_now}

But when I start from the empty array:

empty = [] of Hash(String, String | Time | Int64)

and try to add new element, an error appears:

empty << {"a" => "Aa", "b" => 66, "c" => Time.now}
# no overload matches 'Array(Hash(String, Int64 | String | Time))#<<' 
# with type Hash(String, Int32 | String | Time)

Could you please explain what I do wrong?

Link to sample code

Upvotes: 0

Views: 590

Answers (1)

Sergey Fedorov
Sergey Fedorov

Reputation: 4430

You have a little mistake. You defined hash with String, Int64 | String | Time but trying to add hash with String, Int32 | String | Time.

Just change Int64 to Int32 and this will be work, see fixed example — https://play.crystal-lang.org/#/r/6185

Upvotes: 2

Related Questions