Crystal v0.24.1 with macOS 11.12.6
abstract class Base
abstract def call
end
class A1 < Base
def call; end
end
class B2 < Base
def call; end
end
cls = [] of Base.class
cls << A1
cls << B2
cls2 = {} of String => Base.class
cls2["1"] = A1
cls2["2"] = B2
cls3 = {} of String => Array(Base.class)
cls3["1"] = [A1]
cls3["2"] = [B2]
cls4 = {} of String => Array(Base)
cls4["1"] = [A1.new]
cls4["2"] = [B2.new]
puts cls # => works
puts cls2 # => works
puts cls3 # => throws an exceptions
puts cls4 # => throws an exceptions, too
instance variable '@value' of Hash::Entry(String, Array(Base:Class)) must be Array(Base:Class), not Array(A1:Class)
instance variable '@value' of Hash::Entry(String, Array(Base)) must be Array(Base), not Array(A1)
But use as force to Base class it will pass
cls3 = {} of String => Array(Base.class)
cls3["1"] = [A1.as(Base.class)]
cls3["2"] = [B2.as(Base.class)]
cls4 = {} of String => Array(Base)
cls4["1"] = [A1.new.as(Base)]
cls4["2"] = [B2.new.as(Base)]
you should win a prize, probably person number 1_000_000 to report this 馃帀
i decided to write faq about this, question aksed over and over and over and over:
https://github.com/crystal-lang/crystal/wiki/FAQ#why-my-array-literal-doesnt-work