Conversation
similar!(pool, x) previously always returned a raw Array via _acquire_impl!, even when x was a SubArray/ReshapedArray from acquire_view!. Add Array-specific dispatch to _similar_impl! so plain Arrays route to _acquire_impl! while view-like AbstractArrays route to _acquire_view_impl!.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #40 +/- ##
=======================================
Coverage 96.03% 96.04%
=======================================
Files 14 14
Lines 3232 3240 +8
=======================================
+ Hits 3104 3112 +8
Misses 128 128
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR fixes similar!(pool, x) so it preserves “view semantics” when the template x is a view returned by acquire_view!, instead of always returning a raw Array.
Changes:
- Split
_similar_impl!dispatch to treatx::Arraydifferently from otherAbstractArrayinputs. - Route non-
Arraytemplates through_acquire_view_impl!so views stay views. - Add tests covering
similar!behavior for 1D/2D views, type changes, and dimension changes.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/convenience.jl |
Updates _similar_impl! dispatch to return pooled Array for Array templates and pooled views for non-Array templates. |
test/test_convenience.jl |
Adds a dedicated testset ensuring similar! preserves view-ness when the input is a view. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Closing: |
Summary
similar!(pool, x)always returned a rawArrayeven whenxwas a view fromacquire_view!. This broke the semantic contract thatsimilar!should preserve the "kind" of its input array.Bug
All 4
_similar_impl!methods unconditionally called_acquire_impl!, ignoring whether the input was a view.Fix
Added
Array-specific dispatch to_similar_impl!(4 methods -> 8 methods):_similar_impl!(pool, x::Array, ...)->_acquire_impl!(returnsArray)_similar_impl!(pool, x::AbstractArray, ...)->_acquire_view_impl!(returns view)Julia's dispatch guarantees
Array(concrete type) is always matched beforeAbstractArray, so existing behavior for plain arrays is unchanged.