Simulate Tuple{Vararg{T1,N},T2} where{T1, N, T2}

It seems that Vararg is not supported except at last place, but I want its behavior.
What I am trying to do is to preallocate a vector of views into an Array, so that each view can be reused instead of having to be recreated many times. The detailed motivation is discussed here.

Here is what I wish I could be writing:

using EllipsisNotation
const SliceAll = Base.Slice{Base.OneTo{Int}}
const View{T, N} = SubArray{T,N-1,Array{T,N},Tuple{Vararg{SliceAll,N-1},Int},true} where {T,N}
struct Views{T,N}
    x::Vector{View{T,N}}
    Views{T,N}(x::AbstractArray{T,N}) = new([view(x, .., i) for i in 1:size(x)[end]])
end

So it is the Tuple{Vararg{SliceAll,N-1},Int} part that is blocking me now.
Is there a way around this? Any suggestion appreciated!

The standard workaround here is something like this:

struct Views{V}
    x::V
end
function Views(x::AbstractArray)
    v = [view(x, .., i) for i in 1:size(x)[end]]
    Views{typeof(v)}(v)
end

That is, just let Julia figure it out with a less-restrictive type parameter.

2 Likes

Works!
I really need to fix my brain and do away the jinx that makes me explicitly type everything…

You’ll feel free like a bird once you do :slight_smile:

1 Like