The Crystal Programming Language Forum

Inference of nested empty collection?

Hey, cannot search in the forum apparently, please excuse if this has been asked before.

In Nim, this compiles:

import tables

var table: Table[string, seq[int]]
table["foo"] = @[]

The compiler determines that the sequence being assigned to the “foo” key cannot be of any other type than seq[int], so it infers it and moves on.

However, as we know, the equivalent in Crystal does not compile:

hash = {} of String => Array(Int)
hash["foo"] = [] # Error: for empty arrays use '[] of ElementType'

Is there a fundamental reason for this?

The main reason is probably that it’s not trivial to implement =)

However, a feature discussion has started just a couple of days ago: https://github.com/crystal-lang/crystal/issues/10188

1 Like