Should this error exist for abstract classes?

abstract class A
  def initialize; end
  getter abc : String
end

class B < A
  def initialize
    @abc = "B"
    super()
  end
end

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

There was a problem expanding macro ‘getter’

Code in test.cr:3:2

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

Regards,
iain

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