Instantiation of abstract classes should raise an error

I noticed that I’m able to instantiate an abstract class through indirection:

abstract class Bar
  @value : String

  def initialize(@value)
  end

  abstract def value
end

classes = [Bar]
result = classes.first.new("blah")
puts result # => #<Bar:0x103ab3e80>

however Bar.new("blah") raises Error: can't instantiate abstract class Bar

Is this a known behavior? It caused me a little bit of head scratching in something I was working on until I realized that my class was abstract.

Stranger was that somewhere down the line assigning the result of the the abstract class instantiation to a instance variable crash with the message: Program received and didn't handle signal ILL (4)

1 Like

Known issue. See Abstract classes can be created via class type · Issue #3835 · crystal-lang/crystal · GitHub.

3 Likes