Skip to content
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

fail early for FixedSizeArray subtypes we can't handle #99

Merged
merged 1 commit into from
Feb 11, 2025

Conversation

nsajko
Copy link
Collaborator

@nsajko nsajko commented Feb 9, 2025

Before this PR collect_as had to use a type assert on the return value to make sure the return value is of the type requested by the user. This is necessary because of UnionAll types with non-default constraints on their type parameters, such as FixedSizeVector{T} where {T <: Number}, because accessing the specific constraint is not currently possible in Julia in a supported way.

This change removes the type assert, instead throwing early, as soon as it is recognized that the requested return type is not among the allowed types, which are known-good, in the sense that the return value we produce ends up being of the requested type as expected.

Benefits:

  • a type assert of nonconcrete type is not required any more (those can be expensive)
  • the throw happens earlier than before

Future PRs will possibly apply the same mechanic to some constructors, in addition to collect_as.

Before this PR `collect_as` had to use a type assert on the return
value to make sure the return value is of the type requested by the
user. This is necessary because of UnionAll types with non-default
constraints on their type parameters, such as
`FixedSizeVector{T} where {T <: Number}`, because accessing the
specific constraint is not currently possible in Julia in a supported
way.

This change removes the type assert, instead throwing early, as soon as
it is recognized that the requested return type is not among the
allowed types, which are known-good, in the sense that the return value
we produce ends up being of the requested type as expected.

Benefits:
* a type assert of nonconcrete type is not required any more (those can
  be expensive)
* the throw happens earlier than before

Future PRs will possibly apply the same mechanic to some constructors,
in addition to `collect_as`.
Copy link

codecov bot commented Feb 9, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.07%. Comparing base (b8c23e8) to head (abfe286).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #99      +/-   ##
==========================================
+ Coverage   95.96%   96.07%   +0.11%     
==========================================
  Files           3        3              
  Lines         248      255       +7     
==========================================
+ Hits          238      245       +7     
  Misses         10       10              

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

@nsajko nsajko merged commit 30e318f into JuliaArrays:main Feb 11, 2025
11 checks passed
@nsajko nsajko deleted the allowed_constructors branch February 11, 2025 09:32
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