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

Avoid invalidations by replacing some convert methods by convenience constructors #335

Merged
merged 1 commit into from
Dec 29, 2022

Conversation

fingolfin
Copy link
Contributor

@fingolfin fingolfin commented Dec 28, 2022

... by convenience constructors. Indeed, from the Julia stdlib, we see
the following convert methods, which imply that these constructors
are fully sufficient:

convert(::Type{T}, x::T)      where {T<:Number} = x
convert(::Type{T}, x::Number) where {T<:Number} = T(x)::T

Note that Julia often inserts convert calls into code automatically, so not adding the above avoids a numerous method invalidations in other packages.

This is progress on issue #278.

@fingolfin
Copy link
Contributor Author

I was wondering why this did not work as intended in Julia 1.8 compared to 1.9 -- the reason is that in 1.8 they still have convert(::Type{T}, x::Number) where {T<:Number} = T(x) which was improved by adding the type assertion at the end in 1.9/1.10 (i.e., convert(::Type{T}, x::Number) where {T<:Number} = T(x)::T).

I'll see if I can use that to improve the methods here.

... by convenience constructors. Indeed, from the Julia stdlib, we see
the following convert methods, which imply that these constructors
are fully sufficient:

    convert(::Type{T}, x::T)      where {T<:Number} = x
    convert(::Type{T}, x::Number) where {T<:Number} = T(x)::T

Note that Julia often inserts `convert` calls into code automatically,
so not adding the above avoids a numerous method invalidations in
*other* packages.
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.

None yet

2 participants