使用具有流敏感性的联合类型
Using union types with flow sensitivity
我正在尝试将联合类型与 is_a?
一起用于流量控制,但我仍然遇到冰糕错误。我也尝试过转换,但我仍然 运行 陷入同样的错误,即:
Method to_hash does not exist on T::Array[T.untyped] component of T.any(T::Array[T.untyped], T::Hash[Symbol, T.untyped]) https://srb.help/7003
我有以下结构:
class PostProcessingMethod < T::Struct
prop :method_name, Symbol
prop :args, T.any(Array, T::Hash[Symbol, T.untyped]), default: []
prop :changed_fields, T::Array[String], default: []
prop :all, T::Boolean, default: false
prop :force, T::Boolean, default: false
end
并且我在(当前)看起来像这样的方法中使用它:
sig { params(post_processing_methods: T::Array[Documents::PostProcessingMethod]).void }
def call(post_processing_methods)
post_processing_methods.each do |post_processing_method|
next unless should_call_method?(post_processing_method)
if @object.respond_to?(post_processing_method.method_name)
if post_processing_method.args.is_a?(Array)
@object.send(post_processing_method.method_name, *post_processing_method.args)
elsif post_processing_method.args.is_a?(Hash)
@object.send(post_processing_method.method_name, **post_processing_method.args)
end
end
end
end
我尝试合并 T.cast
以确保 sorbet 知道它是 elsif 中的哈希,但这似乎没有什么不同。
我的期望是 is_a?
应该让 sorbet 知道 post_processing_method
是 elseif
中的 Hash
。但如果不是这种情况,T.cast
肯定会处理这个问题。
在发布的代码中,每个 .args
调用都被视为一个新变量。如果您在局部变量中捕获返回值,则流量敏感度将起作用。
查看 sorbet.run
上的示例
我正在尝试将联合类型与 is_a?
一起用于流量控制,但我仍然遇到冰糕错误。我也尝试过转换,但我仍然 运行 陷入同样的错误,即:
Method to_hash does not exist on T::Array[T.untyped] component of T.any(T::Array[T.untyped], T::Hash[Symbol, T.untyped]) https://srb.help/7003
我有以下结构:
class PostProcessingMethod < T::Struct
prop :method_name, Symbol
prop :args, T.any(Array, T::Hash[Symbol, T.untyped]), default: []
prop :changed_fields, T::Array[String], default: []
prop :all, T::Boolean, default: false
prop :force, T::Boolean, default: false
end
并且我在(当前)看起来像这样的方法中使用它:
sig { params(post_processing_methods: T::Array[Documents::PostProcessingMethod]).void }
def call(post_processing_methods)
post_processing_methods.each do |post_processing_method|
next unless should_call_method?(post_processing_method)
if @object.respond_to?(post_processing_method.method_name)
if post_processing_method.args.is_a?(Array)
@object.send(post_processing_method.method_name, *post_processing_method.args)
elsif post_processing_method.args.is_a?(Hash)
@object.send(post_processing_method.method_name, **post_processing_method.args)
end
end
end
end
我尝试合并 T.cast
以确保 sorbet 知道它是 elsif 中的哈希,但这似乎没有什么不同。
我的期望是 is_a?
应该让 sorbet 知道 post_processing_method
是 elseif
中的 Hash
。但如果不是这种情况,T.cast
肯定会处理这个问题。
在发布的代码中,每个 .args
调用都被视为一个新变量。如果您在局部变量中捕获返回值,则流量敏感度将起作用。
查看 sorbet.run
上的示例