Crystal Lang:无法推断 Rule 的实例变量“@hash_value”的类型
Crystal Lang: Can't infer the type of instance variable '@hash_value' of Rule
这是我的 crystal 代码片段,但我认为它就在那里:
# spec_helper.rb
def fixtures
rawhash = JSON.parse(File.read("spec/fixtures/state3.json"))
rules = rawhash["rules"]
id = rules.as_h.keys[0]
hash = rules[id]
return id, hash
end
# rule_spec.rb
key, hash = fixtures
rule = Rule.new(key, hash)
rule.array(["name"])[0].must_equal "RpsIphone"
# rule.rb
class Rule < HueResource
getter :detail, :on, :name, :lights
def initialize(key, hashvalue)
super(key, hashvalue)
@detail = "#{hashvalue["conditions"].length} conds => #{hashvalue["actions"].length} acts"
@state.merge! ({"on" => hash_value["status"], "name" => @name, "detail" => @detail})
gen_reskey("r")
end
...
end
# hue resource.rb
class HueResource
def initialize(key, hash_value)
@hash_value = hash_value.as_h
@lastupdated = @hash_value["state"]["lastupdated"]
@detail = hash_value["type"]
@name = hash_value["name"]
@state = { "key" => key, "lastupdated" => @lastupdated, "detail" => @detail, "name" => @name}
end
def gen_reskey(detail)
@state.merge!({ "id" => detail + @state["key"]})
end
end
这是错误:
[Running] crystal "/Users/pitosalas/mydev/crystalplay/spec/rule_spec.cr"
Error in spec/rule_spec.cr:7: instantiating 'Rule.class#new(String, JSON::Any)'
rule = Rule.new(key, hash)
[32;1m^~~[0m
in src/rule.cr:7: instantiating 'super(String, JSON::Any)'
super(key, hashvalue)
[32;1m^~~~~[0m
in src/hue_resource.cr:3: [1mCan't infer the type of instance variable '@hash_value' of Rule
The type of a instance variable, if not declared explicitly with
`@hash_value : Type`, is inferred from assignments to it across
the whole program.
现在在我看来,在构造函数中,@hash_value 被分配给 hash_value.as_h,因此它会从那里知道类型。
也可以随时指出 Crystal 文体或惯用评论!
编译器无法从 return 类型的方法调用(如 hash_value.as_h
)中推断出 ivar 类型。 reference of Type inference 列出了编译器可以推断 ivar 类型的所有规则。
从方法调用中推断类型可能并非不可能,但更复杂。它可能会在某个时候出现在语言中,但现在您必须使用显式类型注释,例如 @hash_value : Hash(JSON::Any, JSON::Any)
.
这是我的 crystal 代码片段,但我认为它就在那里:
# spec_helper.rb
def fixtures
rawhash = JSON.parse(File.read("spec/fixtures/state3.json"))
rules = rawhash["rules"]
id = rules.as_h.keys[0]
hash = rules[id]
return id, hash
end
# rule_spec.rb
key, hash = fixtures
rule = Rule.new(key, hash)
rule.array(["name"])[0].must_equal "RpsIphone"
# rule.rb
class Rule < HueResource
getter :detail, :on, :name, :lights
def initialize(key, hashvalue)
super(key, hashvalue)
@detail = "#{hashvalue["conditions"].length} conds => #{hashvalue["actions"].length} acts"
@state.merge! ({"on" => hash_value["status"], "name" => @name, "detail" => @detail})
gen_reskey("r")
end
...
end
# hue resource.rb
class HueResource
def initialize(key, hash_value)
@hash_value = hash_value.as_h
@lastupdated = @hash_value["state"]["lastupdated"]
@detail = hash_value["type"]
@name = hash_value["name"]
@state = { "key" => key, "lastupdated" => @lastupdated, "detail" => @detail, "name" => @name}
end
def gen_reskey(detail)
@state.merge!({ "id" => detail + @state["key"]})
end
end
这是错误:
[Running] crystal "/Users/pitosalas/mydev/crystalplay/spec/rule_spec.cr"
Error in spec/rule_spec.cr:7: instantiating 'Rule.class#new(String, JSON::Any)'
rule = Rule.new(key, hash)
[32;1m^~~[0m
in src/rule.cr:7: instantiating 'super(String, JSON::Any)'
super(key, hashvalue)
[32;1m^~~~~[0m
in src/hue_resource.cr:3: [1mCan't infer the type of instance variable '@hash_value' of Rule
The type of a instance variable, if not declared explicitly with
`@hash_value : Type`, is inferred from assignments to it across
the whole program.
现在在我看来,在构造函数中,@hash_value 被分配给 hash_value.as_h,因此它会从那里知道类型。
也可以随时指出 Crystal 文体或惯用评论!
编译器无法从 return 类型的方法调用(如 hash_value.as_h
)中推断出 ivar 类型。 reference of Type inference 列出了编译器可以推断 ivar 类型的所有规则。
从方法调用中推断类型可能并非不可能,但更复杂。它可能会在某个时候出现在语言中,但现在您必须使用显式类型注释,例如 @hash_value : Hash(JSON::Any, JSON::Any)
.