Julia 花括号内或花括号外的'where'关键字之间的差异
两者的区别是什么Julia 花括号内或花括号外的'where'关键字之间的差异,julia,where-clause,Julia,Where Clause,两者的区别是什么 function foo(a::Adjoint{Float64, Matrix{T}} where T) return 0 end 及 (请注意花括号的位置。) 似乎在这两种情况下,函数都会接受类型为T?我不知道这两个函数之间的区别是什么。第一个是unionall类型,第二个是具体类型: julia> isconcretetype(Adjoint{Float64, Matrix{T} where T}) true julia> isconcretetyp
function foo(a::Adjoint{Float64, Matrix{T}} where T)
return 0
end
及
(请注意花括号的位置。)
似乎在这两种情况下,函数都会接受类型为
T
?我不知道这两个函数之间的区别是什么。第一个是unionall类型,第二个是具体类型:
julia> isconcretetype(Adjoint{Float64, Matrix{T} where T})
true
julia> isconcretetype(Adjoint{Float64, Matrix{T}} where T)
false
julia> (Adjoint{Float64, Matrix{T}} where T) isa Core.UnionAll
true
第一个是无限的伴随集,它包裹着某种类型的矩阵
。换句话说,我们可以想象用伪代码表示无限集,如下所示:
Set([所有可能的类型中T的伴随{Float64,T}])
而第二个是某种类型的矩阵
,或者换句话说:
伴随{Float64,任意类型的矩阵}
你几乎总是想要前者,而不是后者。几乎没有理由构造可以包含任何矩阵的伴随,通常您只需要一种类型的伴随
向量的差异更为明显:
Vector{Vector{T}},其中T
是表示相同类型向量的所有向量的unionall
Vector{Vector{T},其中T}
是包含特定元素类型的向量Vector{T},其中T
这里有一个例子说明了这种区别,对于
Vector{Vector{T}where T}
vsVector{Vector{T}where T
的简单情况
首先制作一些类型别名:
julia> const InsideWhere = Vector{Vector{T} where T}
Array{Array{T,1} where T,1}
julia> const OutsideWhere = Vector{Vector{T}} where T
Array{Array{T,1},1} where T
如果可能,默认数组构造函数将元素升级为公共类型:
julia> x = [[1, 2], [1.2, 3.4]]
2-element Array{Array{Float64,1},1}:
[1.0, 2.0]
[1.2, 3.4]
julia> x isa InsideWhere
false
julia> x isa OutsideWhere
true
但通过使用a,我们可以避免自动升级:
julia> y = ( Vector{T} where T )[[1, 2], [1.2, 3.4]]
2-element Array{Array{T,1} where T,1}:
[1, 2]
[1.2, 3.4]
julia> y isa InsideWhere
true
julia> y isa OutsideWhere
false
谢谢对于阅读本文的人来说,这是很有帮助的:其中一个返回0
,另一个返回1
=P
julia> x = [[1, 2], [1.2, 3.4]]
2-element Array{Array{Float64,1},1}:
[1.0, 2.0]
[1.2, 3.4]
julia> x isa InsideWhere
false
julia> x isa OutsideWhere
true
julia> y = ( Vector{T} where T )[[1, 2], [1.2, 3.4]]
2-element Array{Array{T,1} where T,1}:
[1, 2]
[1.2, 3.4]
julia> y isa InsideWhere
true
julia> y isa OutsideWhere
false