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

Fix pairwise for type-unstable corner case function #772

Merged
merged 3 commits into from
Mar 15, 2022
Merged

Conversation

nalimilan
Copy link
Member

promote_type is not a completely correct way of computing an upper bound for the return eltype. Use the same strategy as map and broadcast in Base instead (JuliaLang/julia#30485), but with promote_eltype rather than promote_typejoin. We can still use typejoin_union_tuple since promotion does not happen inside tuple types.

On Julia versions before 1.6 we would have to copy the full definition of typejoin_union_tuple, which is quite complex, so instead fall back to inferring eltype Any (inference wasn't so good anyway on these versions).

Fixes #771.

`promote_type` is not a completely correct way of computing an upper bound
for the return eltype. Use the same strategy as `map` and `broadcast` in
Base instead, but with `promote_eltype` rather than `promote_typejoin`.
We can still use `typejoin_union_tuple` since promotion does not happen
inside tuple types.

On Julia versions before 1.6 we would have to copy the full definition of
`typejoin_union_tuple`, which is quite complex, so instead fall back to
inferring eltype `Any` (inference wasn't so good anyway on these versions).
src/pairwise.jl Outdated
end
end
else
promote_type_union(::Type) = Any
Copy link
Contributor

Choose a reason for hiding this comment

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

do we need to be that defensive? If passed type is not a union then I guess we can return it - right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point. Actually the only case that would be hard to support is Tuple types. I've refactored the code to improve this.

src/pairwise.jl Outdated Show resolved Hide resolved
elseif T <: Tuple
return typejoin_union_tuple(T)
else
return T
Copy link
Contributor

Choose a reason for hiding this comment

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

Is line 148 covered in tests?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's covered as promote_type_union calls itself recursively, but I've added two tests for clarity.

@nalimilan nalimilan merged commit a1b02d8 into master Mar 15, 2022
@nalimilan nalimilan deleted the nl/pairwise branch March 15, 2022 10:20
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.

Failed type assertion in pairwise
2 participants