FixedSizeArrays.jl
FixedSizeArrays.jl copied to clipboard
Range-indexing FixedSizeArrays must/could return other FixedSizeArrays or "SubFixedSizeArray"s
+1 will work on this in the next days
I just ran into this as well. We should definitely return a fixed size array for slices, at the moment a tuple comes out:
julia> Vec(1,2,3)[1:2]
(1,2)
Then, there is also the question of type stability of such indexing.
A workaround I've been using (told by @SimonDanisch ) is using Val-encapsulated indexes: Vec(1,2,3)[Val{1:2}] and generated functions:
@generated function Base.getindex{R}(v::Vec, i::Type{Val{R}})
:(Vec(tuple($([:(v._[$i]) for i in R]...))))
end
@generated function Base.getindex{Ri, Rj}(m::Mat, ::Type{Val{Ri}}, ::Type{Val{Rj}})
if isa(Rj, Integer)
:(Vec(tuple($([:(m._[$Rj][$i]) for i in Ri]...))))
else
:(Mat(tuple($([:(tuple($([:(m._[$j][$i]) for i in Ri]...))) for j in Rj]...))))
end
end
But for a first approach we may have to forget about type stability, and just improve current getindex methods of https://github.com/SimonDanisch/FixedSizeArrays.jl/blob/master/src/indexing.jl#L1-L6.
BTW, this Vec(Vec(1,2,3)[1:2]) already works. It doesn't work well for Mats though.
I could give it another go now that we have a "better" similar function ;)
But type instability will remain, as long as we don't use something like Val or get const propagation.
Although, from vec[(1,2,3)] the type should be inferrable just fine...
We could think about introducing some fixed size range type :D
Good point about the type problems. The tuple approach seems like the best thing we can do at the moment IMHO, though for anything larger than 3 or 4-vectors, the Val approach would make some sense regardless of the ugly syntax.
Constant propagation has been discussed a little at JuliaLang/julia#5560 - I imagine even one pass of this would solve a whole raft of what are currently tricky design problems. Unfortunately doesn't look like it's coming any time soon!