where 作为一种限定,很有意思。
julia> Array{T,1} where T == Array{Any,1} #where后面不能有==
ERROR: syntax: invalid variable expression in "where" around REPL[15]:1
Stacktrace:
[1] top-level scope at REPL[15]:1
julia> Array{T,1} where T
Array{T,1} where T
julia> Array{Any,1} == Array{T,1} where T # 可以
false
如果我要设计一种Dict,key是String,但value是可以是任何Array, 类似于Array{T,1} where T ,要求这个Dict类型,同时可以存放不同类型的Array。也许可以想到:
Dict{String,Array{T,1}} where T; 与Dict{String,Array{T,1}where T}
我的理解:
Dict{String,Array{T,1}} where T; #存放一种类型的数据,不同混同。
因此,你可以选择:
Dict{String,Array{T,1}where T}
既可以Dict{String,Array{Int64,1}},也可以Dict{String,Array{String,1}};
julia> instrs = Dict("YLXF"=>[("IC","IH")],"XXXY"=>["IC"])
Dict{String,Array{T,1} where T} with 2 entries:
"XXXY" => ["IC"]
"YLXF" => [("IC", "IH")]
julia> s = Dict{String,Array{T,1} where T}() ;
julia> setindex!(s,["a"],"a")
Dict{String,Array{T,1} where T} with 1 entry:
"a" => ["a"]
julia> setindex!(s,[("a","b")],"b")
Dict{String,Array{T,1} where T} with 2 entries:
"b" => [("a", "b")]
"a" => ["a"]
julia> Dict{String,Array{T,1} where T} == Dict{String,Array{T,1}} where T
false
julia> A = Dict{String,Array{T,1}} where T
Dict{String,Array{T,1}} where T
julia> typeof(A)
UnionAll
julia> B = Dict{String,Array{T,1} where T}
Dict{String,Array{T,1} where T}
julia> typeof(B)
DataType
where目前还没找到放在{}外的类型,where放在函数后面,这个是比较常见的。
这种情况类似于:
julia> A = Dict{String,Array{T,1}} where T
Dict{String,Array{T,1}} where T
julia> f(a::T) where T = Dict{String,Array{T,1}}()
f (generic function with 1 method)
julia> typeof(6.0)
Float64
julia> typeof(f(6.0))
Dict{String,Array{Float64,1}}
julia> a = Dict{String,Array{T,1}} where T
Dict{String,Array{T,1}} where T
也就是说,a类型的具体化,目前还没找到实现方法。
julia> f(a::T) where T = Array{Array{T where T,1},1}()
f (generic function with 1 method)