Compat.jl icon indicating copy to clipboard operation
Compat.jl copied to clipboard

EachRow/EachCol types (see #32310)

Open simonbyrne opened this issue 6 years ago • 7 comments

Obviously still needs JuliaLang/julia#32310 to be merged, but this is just to show we can monkey-patch the aliases to support older versions of Julia.

cc: @nalimilan

simonbyrne avatar Oct 08 '19 04:10 simonbyrne

Could we also define Compat.AbstractSlices as part of this to "backport" Base.AbstractSlices (see https://github.com/JuliaLang/julia/pull/32310#issuecomment-1124631069)? This will allow packages like JuliennedArrays.jl, ArraysOfArrays.jl to extend that type without depending on non-LTS Julia versions.

oschulz avatar May 12 '22 07:05 oschulz

We could also add

const EachSlice{A,I} = Base.Generator{I,<:(typeof(eachslice(ones(Int32,2,2), dims = 1).f).name.wrapper){A}}
Base.parent(x::EachSlice) = x.f.A

to add support for "legacy" eachslice, in addition to "legacy" eachrow and eachcol. Seems to work fine:

julia> parent(eachslice(rand(3,4,5,6), dims = 3))
3×4×5×6 Array{Float64, 4}:
#...

oschulz avatar May 12 '22 10:05 oschulz

With https://github.com/JuliaLang/julia/pull/32310 finally merged, this could now be picked up again if there is still interest.

martinholters avatar Jun 10 '22 10:06 martinholters

I think we should definitely do this. Ideally with AbstractSlices and EachSlice as well?

oschulz avatar Jun 10 '22 11:06 oschulz

Adding AbstractSlices in the spirit of this PR isn't possible AFAICT as on Julia < 1.9 eachrow and eachcol return Generator objects, which cannot be made to inherit from AbstractSlices. What can be done is adding unexported eachrow, eachcol and eachslice functions that would be copies of what has been added at https://github.com/JuliaLang/julia/pull/32310, i.e returning EachSlice <: AbstractSlices objects.

Probably the best would be to implement both approaches. The former is useful for packages that want to use a special method when users call f(eachcol(x)) on any Julia version. The latter is useful if they want to dispatch on AbstractSlices, and/or call Compat.eachslice themselves.

nalimilan avatar Jun 11 '22 21:06 nalimilan

Adding AbstractSlices in the spirit of this PR isn't possible AFAICT

Ah - I don't want to add it to have eachrow and eachcol as subtypes - that's not possible, I agree. But I think it's important to add it in Compat so that packages like ArraysOfArrays.jl and JuliennedArrays.jl can make their types subtypes of AbstractSlices while still supporting Julia LTS. And so we can do something like

@static if isdefined(Base, :Slices)
    const SlicedArrayLike = AbstractSlices
else
    const SlicedArrayLike = Union{EachRow,EachCol,EachSlice,AbstractSlices}
end

in ArrayInterfaceCore (prototyping this here). That will then allow code that can take advantage of slices array to dispatch on SlicedArrayLike, which will cover the legacy eachrow and eachcol, as well as the Julia v1.9 Slices and subtypes of AbstractSlices.

For that to work, we need an AbstractSlices that's available from Julia v1.6 on, via Compat.

oschulz avatar Jun 12 '22 08:06 oschulz

1.9alpha is out. What's the plan here?

mcabbott avatar Nov 18 '22 03:11 mcabbott

Bump - is there support for adding this to Compat?

oschulz avatar Nov 12 '23 09:11 oschulz