3
votes

say I defined a composite type "Data" as follows:

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

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

It's OK when I run myformat = DataFormat(Float32, 0, 0, C_NULL). But as I run this mydata = Data{Float32}([0.1, 0.2, 0.3], myformat), these errors appear:

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

It seems something broken with julia's size() function, how can I fix it? I'm very new to julia and don't know whether it's a good choice to use "nested" composite type like this?

1

1 Answers

3
votes

It's relatively straightforward to extend size:

julia> import Base.size

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

If you want your datatype to support other operations like iteration and indexing you'll need to also define getindex, start, next, length and done

Edit: See comment below for why start, next, length and done are not necessary.