Should this error exist for abstract classes?

abstract class A
  def initialize; end
  getter abc : String

class B < A
  def initialize
    @abc = "B"

@abc can never be nil because of the abstract class, right? However:

There was a problem expanding macro ‘getter’

Code in

3 | getter abc : String
Called macro defined in macro ‘macro_4536162272’

117 | macro getter(*names, &block)

Which expanded to:

2 |
3 |
4 | @abc : String
Error: instance variable ‘@abc’ of A was not initialized directly in all of the ‘initialize’ methods, rendering it nilable. Indirect initialization is not supported.

Or is there something I’ve missed?

Crystal v1.5.0


That’s intentional. You must initial it in the abstract class. An easy way to do that is to pass it in the initialize method.

1 Like