【问题标题】:TypeError typeassert for custom typealias自定义类型别名的 TypeError typeassert
【发布时间】:2017-04-07 18:42:11
【问题描述】:

我无法理解以下行为

a = [1,2,3]
a::Vector  # works

d = Dict(0=>a)
typealias DictVector{K,V} Dict{K, Vector{V}}
d::DictVector  # fails

错误如下

TypeError: typeassert: expected Dict{K,Array{V,1}}, got 
 Dict{Int64,Array{Int64,1}}
  in include_string(::String, ::String) at loading.jl:441
  in eval(::Module, ::Any) at boot.jl:234
  in (::Atom.##65#68)() at eval.jl:40
  in withpath(::Atom.##65#68, ::Void) at utils.jl:30
  in withpath(::Function, ::Void) at eval.jl:46
  in macro expansion at eval.jl:109 [inlined]
  in (::Atom.##64#67{Dict{String,Any}})() at task.jl:60

但是 Vector 本身就是 typealias Vector{T} Array{T,1} 那么这两种情况之间的决定性区别是什么?

任何澄清都非常受欢迎

【问题讨论】:

    标签: julia type-alias


    【解决方案1】:

    你说得对,应该可以。看起来这是 0.5 中旧类型系统中的一个错误。它已在即将发布的 0.6 版本中修复。

    以下是 0.5 的可能解决方法:

    julia> typealias DictVector{K,V<:Vector} Dict{K,V}
    Dict{K,V<:Array{T,1}}
    
    julia> d::DictVector
    Dict{Int64,Array{Int64,1}} with 1 entry:
      0 => [1,2,3]
    
    julia> isa(d, DictVector)
    true
    

    【讨论】:

    • 非常感谢您的澄清。我很高兴这应该可以工作
    猜你喜欢
    • 1970-01-01
    • 2012-03-04
    • 1970-01-01
    • 2022-06-28
    • 2019-06-21
    • 1970-01-01
    • 2016-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多