Skip to content

Commit

Permalink
Best practice on properly support OffsetArrays
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnychen94 committed May 17, 2022
1 parent d8c8d42 commit 9be698b
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,21 @@ julia> Origin(AO)(D)

Here, `Origin(AO)` is able to automatically infer and use the indices of `AO`.

## FAQ

**How to properly write functions that support OffsetArrays?**

It turns out that many packages don't handle custom axes well

- Don't loop the vector using explicit `for i in 1:length(x)` -- use `for i in eachindex(x)` instead.
- For multi-dimensional array, there are also `LinearIndices` and `CartesianIndices`.
- When comparing the arrays, check the `axes(A) == axes(B)` instead of `size(A) == size(B)`.
- Use `OffsetArrays.no_offset_view(x)`, handle it in 1-based indexing scenario, and optionally add the
offsets back. But don't overuse this, because
- not every operation have a well-defined and unambiguous behavior for generic offset array. If that
is the case, use `Base.require_one_based_indexing` to early check the axes and early throw errors.
Let the user to the decision.

<!-- badges -->

[pkgeval-img]: https://juliaci.github.io/NanosoldierReports/pkgeval_badges/O/OffsetArrays.svg
Expand Down

0 comments on commit 9be698b

Please sign in to comment.