Thank you, Tim, I understand the motivation for the approach in the link
you sent.
I'm still unable to create the type that I want. I actually want two arrays
in my type, one 2d and the other 3d.
First though, I'm still stuck on just the 2d when I try to implement your
suggestion:
julia> type MyType{T,N,A<:AbstractArray} <: AbstractArray{T,N}
var::A
end
julia> MyType{T,N}(var::AbstractArray{T,N}) = MyType{T,N,typeof(var)}(var)
MyType{T,N,A<:AbstractArray{T,N}}
julia> aa = MyType(zeros(2,3))
Error showing value of type MyType{Float64,2,Array{Float64,2}}:
ERROR: MethodError: `size` has no method matching size(::MyType{Float64,2,
Array{Float64,2}})
Closest candidates are:
size{T,n}(::AbstractArray{T,n}, ::Any)
size(::Any, ::Integer, ::Integer, ::Integer...)
in showarray at show.jl:1231
in anonymous at replutil.jl:29
in with_output_limit at ./show.jl:1271
in writemime at replutil.jl:28
in display at REPL.jl:114
in display at REPL.jl:117
[inlined code] from multimedia.jl:151
in display at multimedia.jl:162
in print_response at REPL.jl:134
in print_response at REPL.jl:121
in anonymous at REPL.jl:624
in run_interface at ./LineEdit.jl:1610
in run_frontend at ./REPL.jl:863
in run_repl at ./REPL.jl:167
in _start at ./client.jl:420
julia>
Performance is important, but it's not clear what I should use in place of
abstract types; I've tried replacing AbstractArray with just Array but that
does not appear to work.
On Monday, November 16, 2015 at 1:03:05 PM UTC+1, Tim Holy wrote:
>
> 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
>
>