Skip to content

Determine eltype and ndims type parameter positions automatically #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 12, 2025

Conversation

mtfishman
Copy link
Member

@mtfishman mtfishman commented Feb 10, 2025

This brings back the functionality removed in #26 but with a more robust strategy.

This uses the fact that when an AbstractArray subtype with unconventional type parameter positions for eltype and ndims is defined, the information of where those type parameters are defined can be determined by matching the names of the TypeVars of the type parameters to the same name in the AbstractArray type output by InteractiveUtils.supertypes, for example:

julia> struct MyBoolArray{X,Y,Z,B} <: AbstractArray{Bool,B} end

julia> supertypes(MyBoolArray)
(MyBoolArray, AbstractArray{Bool}, Any)

julia> Base.unwrap_unionall(supertypes(MyBoolArray)[1]).parameters
svec(X, Y, Z, B)

julia> Base.unwrap_unionall(supertypes(MyBoolArray)[2]).parameters
svec(Bool, B)

So you can see that we can determine that the 4th parameter of MyBoolArray must be the ndims type parameter, since it has a name B matching the ndims parameter of its AbstractArray supertype.

In principle this can work with non-AbstractArray types (using the internal function TypeParameterAccessors.position_from_supertype) but for now that is opt-in and an internal feature.

@mtfishman mtfishman requested review from lkdvos and kmp5VT February 10, 2025 20:40
Copy link

codecov bot commented Feb 10, 2025

Codecov Report

Attention: Patch coverage is 79.16667% with 5 lines in your changes missing coverage. Please review.

Project coverage is 83.41%. Comparing base (24b3add) to head (fc8c8df).
Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
src/base/abstractarray.jl 40.00% 3 Missing ⚠️
src/type_parameters.jl 89.47% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #31      +/-   ##
==========================================
+ Coverage   83.08%   83.41%   +0.32%     
==========================================
  Files          14       14              
  Lines         201      217      +16     
==========================================
+ Hits          167      181      +14     
- Misses         34       36       +2     
Flag Coverage Δ
docs 43.92% <30.43%> (-1.53%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@mtfishman mtfishman changed the title Determine AbstractArray eltype and ndims type parameter positions automatically Determine eltype and ndims type parameter positions automatically Feb 10, 2025
Copy link
Contributor

@lkdvos lkdvos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking the time to clarify, it's definitely easier to follow for me like this 😄

@mtfishman
Copy link
Member Author

Thanks for taking the time to clarify, it's definitely easier to follow for me like this 😄

I was also getting confused by the previous code I wrote as I was rewriting it so that wasn't a good sign!

@mtfishman mtfishman merged commit 2402cc8 into main Feb 12, 2025
16 of 17 checks passed
@mtfishman mtfishman deleted the abstractarray_fallback branch February 12, 2025 16:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants