我有这么一个数据结构
struct Term{N}
comps::SVector{N, <:Integer}
end
这是不是一个不好的参数类型设计?因为comps
把整个Term
类型变得不稳定了。
一个具体案例:
struct Conjunction{O<:Temporal,N} <: Compound
comps::SVector{N,<:Term}
end
const ConjF = Conjunction{Forward}
const ConjP = Conjunction{Parallel}
function setcondition(cond, i, v)
@nospecialize cond v
@match (cond, v) begin
(::ConjF, ::ConjF) || (::ConjP, ::ConjP) => begin
constructorof(typeof(cond))([cond[1:i-1]..., v.comps..., cond[i+1:end]...])
end
_ => constructorof(typeof(cond))([cond[1:i-1]..., v, cond[i+1:end]...])
end
end
在REPL
里运行
julia> c1 = Conjunction{Forward}([Word(:A), Word(:B)])
"(&/,A,B)"
julia> c2 = Conjunction{Forward}([Word(:C), Word(:D)])
"(&/,C,D)"
julia> Junars.setcondition(c1, 1, c2)
"(&/,C,D,B)"
julia> @code_warntype Junars.setcondition(c1, 1, c2)
MethodInstance for Junars.setcondition(::Conjunction{Forward, 2}, ::Int64, ::Conjunction{Forward, 2})
from setcondition(cond, i, v) in Junars at D:\codes\Junars.jl\src\rules\conditional.jl:121
Arguments
#self#::Core.Const(Junars.setcondition)
cond::Conjunction{Forward, 2}
i::Int64
v::Conjunction{Forward, 2}
Locals
355::Union{}
354::Union{}
353::Conjunction{Forward, 2}
352::Conjunction{Forward, 2}
351::Tuple{Conjunction{Forward, 2}, Conjunction{Forward, 2}}
return#349::Union{Nothing, Conjunction{Forward}}
Body::Conjunction{Forward} # Red!!!
最后的结果当然是类型不稳定的,标红了。