Hmm, seems like this gives an error when constructing a type:
julia> ArrayWrapper(rand(3, 4))
ERROR: MethodError: `convert` has no method matching convert(::Type{
ArrayWrapper{T,N,AT<:AbstractArray{T,N}}}, ::Array{Float64,2})
This may have arisen from a call to the constructor ArrayWrapper{T,N,AT<:
AbstractArray{T,N}}(...),
since type constructors fall back to convert methods.
Closest candidates are:
call{T}(::Type{T}, ::Any)
convert{T}(::Type{T}, ::T)
in call at essentials.jl:57
Moving constructor outside type definition doesn't work too.
On Monday, November 30, 2015 at 12:00:10 PM UTC+3, Mauro wrote:
>
> > I'm trying to do something like this (which doesn't compile in its
> current
> > form):
> >
> > type ArrayWrapper{T,N,AT <: AbstractArray{T,N}} <: AbstractArray{T,N}
> > arr::AT{T,N}
> > end
> >
> > That is:
> >
> > - wrapper around any type AT inherited from AbstractArray{T,N}
> > - wrapper should be itself parametrized by T and N
> > - wrapper should itself extend AbstractArray{T,N}
> >
> > The code above currently gives an error:
> >
> > ERROR: TypeError: instantiate_type: expected TypeConstructor, got
> TypeVar
> >
> >
> > and the closest thing that works looks like this:
> >
> > type ArrayWrapper{T,N,AT <: AbstractArray} <: AbstractArray{T,N}
> > arr::AT
> > end
> >
> > which looks too unconstrained.
> >
> > Is there a way to get what I need?
>
> If you're ok with using immutable (note, you can still modify the array,
> just not its binding), this should work:
>
> immutable ArrayWrapper{T,N,AT <: AbstractArray} <: AbstractArray{T,N}
> arr::AT
> function ArrayWrapper(arr)
> @assert T==eltype(arr) && length(size(arr))==N
> new(arr)
> end
> end
>