我正在尝试在我的模块中定义一个类变量,但是我收到的错误是@@ 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
我尝试了不同的方法来定义这个,任何想法可能导致这个?
答案 0 :(得分:4)
似乎extend
不包含类变量。 language reference没有提到这一点,但它看起来是一个错误(请参阅#4066)。
但是如果你也include Bla
(虽然这可能不是你想要的)或者在extended
宏钩子中定义类变量,它会起作用:
macro extended
@@blub : Int32 = 0
end