The Crystal Programming Language Forum

Typing fails if array has one element

class Base
  @@stuff = [] of Base.class
  def self.stuff
    @@stuff
  end
end

class T < Base
end

class A < Base
  @@stuff = [A, T]
end


def dance(t : Base.class)
  pp t.stuff
end

dance(A)

This errors with: Error: class variable '@@stuff' of A is already defined as Array(Base.class) in Base if you remove one of the elements from A@@stuff, but works fine with 2 or more. Why?

What version of crystal are you using? I just tried with 1.0.0 and it worked fine?

1.0.0 as well, did you try removing one of the items from A@@stuff?

Oh sorry I misread.

The reason is that when you have both T and A, the type of that array is reduced to the most common parent, i.e. Base.class. However when you only have one or the other the array is type as either A.class OR T.class. Which isn’t allowed since the array was previously restricted to Base.class, which isn’t the same thing as A.class or T.class.

See Virtual and abstract types - Crystal.

EDIT: Also checkout Inheritance - Crystal.

1 Like

Aha, thank you very much. Is there a way to make the type of the the array accept any subclass? Or force the array in A to be of the Base class?

[T] of Base.class. Can see that 2nd link for more info.

2 Likes