Skip to content

More default_type_parameters definitions #35

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 14, 2025
Merged

Conversation

mtfishman
Copy link
Member

@mtfishman mtfishman commented Feb 12, 2025

This defines missing overloads of default_type_parameters for array types in package extensions.

In addition, it defines a fallback definition using a similar logic to #31. As an example, the package doesn't explicitly define a default_type_parameters overload for LinearAlgebra.Diagonal but with this PR we get:

julia> using TypeParameterAccessors: default_type_parameters

julia> using LinearAlgebra: Diagonal

julia> default_type_parameters(Diagonal)
(Float64, TypeParameterAccessors.UndefinedDefaultTypeParameter())

julia> default_type_parameters(Diagonal, 1)
Float64

julia> default_type_parameters(Diagonal, 2)
ERROR: No default parameter defined at this position.
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:35
 [2] default_type_parameters(::Type{Diagonal}, ::TypeParameterAccessors.Position{2})
   @ TypeParameterAccessors ~/.julia/dev/TypeParameterAccessors/src/type_parameters.jl:258
 [3] default_type_parameters(::Type{Diagonal}, pos::Int64)
   @ TypeParameterAccessors ~/.julia/dev/TypeParameterAccessors/src/type_parameters.jl:253
 [4] top-level scope
   @ REPL[7]:1

julia> default_type_parameters(Diagonal, eltype)
Float64

julia> default_type_parameters(Diagonal, ndims)
ERROR: Position not found.
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:35
 [2] #s1#1
   @ ~/.julia/dev/TypeParameterAccessors/src/type_parameters.jl:76 [inlined]
 [3] var"#s1#1"(T::Any, ::Any, ::Any, supertype_pos::Any)
   @ TypeParameterAccessors ./none:0
 [4] (::Core.GeneratedFunctionStub)(::UInt64, ::LineNumberNode, ::Any, ::Vararg{Any})
   @ Core ./boot.jl:707
 [5] position_from_supertype(type::Type, name::Function)
   @ TypeParameterAccessors ~/.julia/dev/TypeParameterAccessors/src/type_parameters.jl:47
 [6] position(type::Type, name::Function)
   @ TypeParameterAccessors ~/.julia/dev/TypeParameterAccessors/src/type_parameters.jl:37
 [7] default_type_parameters(::Type{Diagonal}, pos::Function)
   @ TypeParameterAccessors ~/.julia/dev/TypeParameterAccessors/src/type_parameters.jl:253
 [8] top-level scope
   @ REPL[9]:1

Note that it only defines default parameters that are defined for a supertype (in this case, AbstractArray defines defaults for eltype and ndims). Undefined parameters are output as TypeParameterAccessors.UndefinedDefaultTypeParameter(), which when accessed by position throws an error.

I'm open to suggestions about other ways to design that. It seems reasonable, at least with the current design, since types may not want to define defaults for every type parameter, though probably we could rethink that design with #27.

Todo:

  • Add tests.

Copy link

codecov bot commented Feb 12, 2025

Codecov Report

Attention: Patch coverage is 73.68421% with 10 lines in your changes missing coverage. Please review.

Project coverage is 82.40%. Comparing base (2402cc8) to head (0b20716).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
ext/TypeParameterAccessorsAMDGPUExt.jl 0.00% 2 Missing ⚠️
ext/TypeParameterAccessorsCUDAExt.jl 0.00% 2 Missing ⚠️
ext/TypeParameterAccessorsMetalExt.jl 0.00% 2 Missing ⚠️
ext/TypeParameterAccessorsStridedViewsExt.jl 0.00% 2 Missing ⚠️
ext/TypeParameterAccessorsoneAPIExt.jl 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #35      +/-   ##
==========================================
- Coverage   83.41%   82.40%   -1.02%     
==========================================
  Files          14       14              
  Lines         217      250      +33     
==========================================
+ Hits          181      206      +25     
- Misses         36       44       +8     
Flag Coverage Δ
docs 39.02% <8.10%> (-4.91%) ⬇️

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 [WIP] More default_type_parameters definitions More default_type_parameters definitions Feb 13, 2025
@mtfishman mtfishman marked this pull request as ready for review February 13, 2025 01:29
@mtfishman mtfishman requested review from lkdvos and kmp5VT February 13, 2025 01:32
Copy link
Collaborator

@kmp5VT kmp5VT left a comment

Choose a reason for hiding this comment

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

Everything looks good to me. Thanks!

@mtfishman mtfishman merged commit 66c238d into main Feb 14, 2025
15 of 17 checks passed
@mtfishman mtfishman deleted the default_type_params branch February 14, 2025 15:39
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.

3 participants