说我定义了一个复合类型“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()函数似乎有些不妥,我该如何解决? 我对朱莉娅很新,不知道使用这样的“嵌套”复合类型是否是一个不错的选择?
答案 0 :(得分:3)
扩展size
:
julia> import Base.size
julia> size(d::Data) = size(d.value)
size (generic function with 51 methods)
如果您希望您的数据类型支持迭代和索引等其他操作,您还需要定义getindex
, start,next,length和done
编辑:请参阅下面的评论,了解为什么不需要开始,下一步,长度和完成。