假设我有类型
immutable X{T}
a::T
end
immutable Y{T}
a::T
end
我想做这样的事
type A{T, U}
x::U{T}
y::T
end
因此实例可以是 A(X(a), a)
或 A(Y(a), a)
它不起作用
LoadError: TypeError: Type{...} 表达式:期望的 Type{T},得到了 TypeVar
正确的做法是什么?
最佳答案
如错误所述,U
是 TypeVar
,而不是 Type
。答案是使 U
成为真实类型:
julia> abstract U{T}
julia> immutable X{T} <: U{T}
a::T
end
julia> immutable Y{T} <: U{T}
a::T
end
julia> type A{T}
x::U{T}
y::T
end
julia> A(X(1),1)
A{Int64}(X{Int64}(1),1)
julia> A(X(1),1.)
ERROR: MethodError: no method matching A{T}(::X{Int64}, ::Float64)
Closest candidates are:
A{T}{T}(::U{T}, ::T) at REPL[4]:2
A{T}{T}(::Any) at sysimg.jl:53
julia> A(Y(1),1)
A{Int64}(Y{Int64}(1),1)
关于julia - 如何在参数类型上定义参数类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41453647/