I have a superclass like so,
abstract class Klass
def initialize
{% for ivar in @type.instance_variables %}
@{{ivar.name}} = {{ivar.type.id}}.new
{% end %}
end
end
and when I subclass it without specifying a constructor, it works as expected.
class OKSubKlass < Klass
property prop : Hash(String, String)
end
However, if I add any constructor at all, I get an error because the superclass’s constructor overloads are no longer detected.
class NotOK < Klass
property prop : Array(Int)
def initialize(arr)
@prop = arr
end
end
# gives this error:
# wrong number of arguments for 'NotOK.new' (given 0, expected 1)
# Overloads are:
# - NotOK.new(arr)
If I define the overload and leave it with just super
, it still doesn’t compile, saying @prop
needs to be nillable.
class StillNotOK < Klass
property prop : Tuple(String)
def initialize
super
end
def initialize(val)
@prop = val
end
end
Is there a workaround or way to write this or have I simply encountered something you can’t do in crystal?