Julia : where与类型限定

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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值