在 julia 中使用嵌套复合类型时,`size` 没有匹配 size(::Data{Float32}) 的方法

`size` has no method matching size(::Data{Float32}) when using nested composite type in julia

假设我定义了一个复合类型 "Data" 如下:

type DataFormat
    format1::DataType
    format2::Uint
    format3::Uint
    format4::Ptr{None}
end

type Data{T} <: AbstractVector{T}
    value::Vector{T}
    format::DataFormat    
end

我运行myformat = DataFormat(Float32, 0, 0, C_NULL)的时候还可以。 但是当我 运行 这个 mydata = Data{Float32}([0.1, 0.2, 0.3], myformat) 时,出现了这些错误:

Error showing value of type Data{Float32}:
ERROR: `size` has no method matching size(::Data{Float32})
in writemime at replutil.jl:18
in display at REPL.jl:117
in display at REPL.jl:120
in display at multimedia.jl:149
in print_response at REPL.jl:139
in print_response at REPL.jl:124
in anonymous at REPL.jl:586
in run_interface at /opt/homebrew-cask/Caskroom/julia/0.3.7/Julia-      0.3.7.app/Contents/Resources/julia/lib/julia/sys.dylib
in run_frontend at /opt/homebrew-cask/Caskroom/julia/0.3.7/Julia-0.3.7.app/Contents/Resources/julia/lib/julia/sys.dylib
in run_repl at /opt/homebrew-cask/Caskroom/julia/0.3.7/Julia-0.3.7.app/Contents/Resources/julia/lib/julia/sys.dylib
in _start at /opt/homebrew-cask/Caskroom/julia/0.3.7/Julia-0.3.7.app/Contents/Resources/julia/lib/julia/sys.dylib

julia 的 size() 函数似乎出了问题,我该如何修复它? 我是 julia 的新手,不知道像这样使用 "nested" 复合类型是否是一个好的选择?

扩展比较简单 size:

julia> import Base.size

julia> size(d::Data) = size(d.value)
size (generic function with 51 methods)

如果您希望您的数据类型支持迭代和索引等其他操作,您还需要定义 getindexstart、next、length 和 done

编辑:请参阅下面的评论,了解为什么不需要 start、next、length 和 done。