在 crystal 中的模块中定义 class 变量会导致错误

defining class vars in modules in crystal causes error

我试图在我的模块中定义一个 class 变量,但我收到一个错误,指出无法推断出 @@blub 的类型,即使我已经明确声明了类型:

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

我得到的错误是

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

我尝试了不同的定义方式,知道是什么原因造成的吗?

似乎extend不包含class变量。 language reference does not mention this, but it looks to be an error (see #4066).

但是,如果您还 include Bla(虽然这可能不是您想要的)或在 extended 宏挂钩中定义 class 变量,它仍然有效:

macro extended
  @@blub : Int32 = 0
end

carc.in