This fixes two problems:
type MyType{T,N,A<:AbstractArray} <: AbstractArray{T,N}
var::A
end
MyType{T,N}(var::AbstractArray{T,N}) = MyType{T,N,typeof(var)}(var)
If performance matters, you should not use abstract types for fields. See
http://docs.julialang.org/en/stable/manual/faq/#how-do-abstract-or-ambiguous-fields-in-types-interact-with-the-compiler
and the section after that.
--Tim
On Monday, November 16, 2015 03:54:15 AM Evan wrote:
> For a 2d array the following works:
>
> type mytype{T}
> var :: AbstractMatrix{T}
> end
>
> julia> t = mytype(zeros(4, 3))
> mytype{Float64}(4x3 Array{Float64,2}:
> 0.0 0.0 0.0
> 0.0 0.0 0.0
> 0.0 0.0 0.0
> 0.0 0.0 0.0)
>
>
>
> But how do I extend this to a 3d array?
>
> julia> t = mytype(zeros(2, 4, 3))
> ERROR: MethodError: `convert` has no method matching convert(::Type{mytype{T
> }}, ::Array{Float64,3})
> This may have arisen from a call to the constructor mytype{T}(...),
> since type constructors fall back to convert methods.
> Closest candidates are:
> call{T}(::Type{T}, ::Any)
> convert{T}(::Type{T}, ::T)
> mytype{T}(::AbstractArray{T,2})
> in call at essentials.jl:56