我正试图在Julia中使用其最佳的参数化和类型。
我有一个构造函数,其参数包含T
数组和Interval{T,Bound{T},Bound{T}}
数组,其中T
是一个真正的类型,Bound
是一个抽象类型,OpenBound是一个NullBound是派生的。
但是当我尝试调用此构造函数时,我收到以下错误:
ERROR: MethodError: no method matching
HyperParameters(::Array{Float64,1},
::Array{Interval{Float64,OpenBound{Float64},NullBound{Float64}},1})
Closest candidates are:
HyperParameters(::Array{T<:Real,1}, ::Array{Interval{T<:Real,A,B} where
B<:Bound{T<:Real} where A<:Bound{T<:Real},1}) where T<:Real
虽然不使用数组的构造函数(仅T
和Interval{T,Bound{T},Bound{T}}
)不会发生这种情况。我正在使用Julia 0.6.2。关于如何解决这个问题的任何想法?谢谢!
西奥
答案 0 :(得分:2)
原因是朱莉娅的类型是不变的。一个更简单的问题示例是,Vector{Vector{Int}}
不是Vector{AbstractVector{Int}}
的子类型,尽管Vector{Int}
是AbstractVector{Int}
的子类型。但Vector{Vector{Int}}
是Vector{<:AbstractVector{Int}}
的子类型。
因此,问题的解决方案是:
julia> abstract type Bound{T} end
julia> struct OpenBound{T} <: Bound{T} end
julia> struct NullBound{T} <: Bound{T} end
julia> struct Interval{T,A,B} end
julia> HyperParameters(::Vector{T}, ::Vector{Interval{T,A,B}}) where {B<:Bound{T}, A<:Bound{T}} where T<:Real = "OK"
HyperParameters (generic function with 1 method)
julia> x = [1.0]
1-element Array{Float64,1}:
1.0
julia> y = [Interval{Float64, OpenBound{Float64}, NullBound{Float64}}()]
1-element Array{Interval{Float64,OpenBound{Float64},NullBound{Float64}},1}:
Interval{Float64,OpenBound{Float64},NullBound{Float64}}()
julia> HyperParameters(x, y)
"OK"
不幸的是你没有发布你的类型定义(我建议你将来做),所以我必须在这里使用人工的。