tpei
tpei

Reputation: 681

defining class vars in modules in crystal causes error

I am trying to define a class variable inside my module, but I'm getting an error that the type of @@blub cannot be inferred, even though I have explicitly declared the type:

module Bla
  @@blub : Int32 = 0

  def add_blub(a : Int32)
    @@blub += a
  end

  def blub
    @@blub
  end
end

class Blub
  extend Bla
end

Blub.add_blub 1
puts Blub.blub

The error I get is

Error in line 17: instantiating 'Blub:Class#add_blub(Int32)'

in line 5: Can't infer the type of class variable '@@blub' of Blub

The type of a class variable, if not declared explicitly with
`@@blub : Type`, is inferred from assignments to it across
the whole program.

The assignments must look like this:

  1. `@@blub = 1` (or other literals), inferred to the literal's type
  2. `@@blub = Type.new`, type is inferred to be Type
  3. `@@blub = Type.method`, where `method` has a return type
     annotation, type is inferred from it
  4. `@@blub = arg`, with 'arg' being a method argument with a
     type restriction 'Type', type is inferred to be Type
  5. `@@blub = arg`, with 'arg' being a method argument with a
     default value, type is inferred using rules 1, 2 and 3 from it
  6. `@@blub = uninitialized Type`, type is inferred to be Type
  7. `@@blub = LibSome.func`, and `LibSome` is a `lib`, type
     is inferred from that fun.
  8. `LibSome.func(out @@blub)`, and `LibSome` is a `lib`, type
     is inferred from that fun argument.

Other assignments have no effect on its type.

Can't infer the type of class variable '@@blub' of Blub

I tried different ways of defining this, any idea what might cause this?

Upvotes: 1

Views: 143

Answers (1)

Johannes Müller
Johannes Müller

Reputation: 5661

It seems that extend does not include class variables. The language reference does not mention this, but it looks to be an error (see #4066).

It works however if you also include Bla (though that's probably not what you want) or define the class variable in an extended macro hook:

macro extended
  @@blub : Int32 = 0
end

carc.in

Upvotes: 4

Related Questions