Unify reshape methods accepting Colon and OneTo
#56850
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently, there are a bunch of
reshapemethods that do identical things. The methods that accept adims::Varargsimply forward theTupletorehape, and there are specialized methods forColons. We may unify all these methods and have only one method to handle bothOneTos andColons, such that any combination of these works. Firstly, this addresses some missing combinations ofdims, as seen in #41069, and secondly, this would reduce the number of methods that packages need to define to avoid ambiguities when they're specializing on the array type. E.g., the method in https://github.com/JuliaArrays/FillArrays.jl/blob/295266cef8fa38813b0f31bed9c3ddb27a4b4de3/src/FillArrays.jl#L275-L279and https://github.com/JuliaArrays/OffsetArrays.jl/blob/8decfce50edeeab1694232f80427ea66c6afc023/src/OffsetArrays.jl#L381-L383seem identical to those inBase, and such ambiguities are better avoided by broadening the specificBasemethods.The current implementation would firstly convert
OneTos indimstoIntegers, and secondly, convertColons to integers through the usual route. Finally, we would callreshape(parent::AbstractArray, dims::Tuple{Vararg{Integer}}), or, in common cases,reshape(parent::AbstractArray, dims::Tuple{Vararg{Int}}).One comment in #41069 was that the method
should be defined instead as
but this would require making
_reshape_uncolonpublic. I've not made this change in this PR, and we may think about this separately.Supersedes and closes #41069.
This PR also generalizes the method
reshape(parent, dims::IntOrInd...)to accept any combinations withColons. This means that packages would typically not need to define this method, and may only specialize the method that accepts aTupleofdims. Unfortunately, this would currently breakOffsetArrays, as this commits type-piracy and defines the generic method (see line 531 oftest/testhelpers/OffsetArrays.jl), but it shouldn't be doing this anyway. A patch to remove this method fromOffsetArrayswould fix the method overwritten error if this is merged.