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 convert bug #320

Closed
wants to merge 3 commits into from
Closed

Fix convert bug #320

wants to merge 3 commits into from

Conversation

dpsanders
Copy link
Member

No description provided.

@@ -17,7 +17,7 @@ convert(::Type{Interval{T}}, x::Bool) where {T} = convert(Interval{T}, Int(x))
convert(::Type{Interval{T}}, x::Real) where {T} = atomic(Interval{T}, x)
convert(::Type{Interval{T}}, x::T) where {T<:Real} = Interval{T}(x)
convert(::Type{Interval{T}}, x::Interval{T}) where {T} = x
convert(::Type{Interval{T}}, x::Interval) where {T} = atomic(Interval{T}, x)
convert(::Type{Interval{T}}, x::Interval{S}) where {T,S} = atomic(Interval{T}, x)

convert(::Type{Interval}, x::Real) = (T = typeof(float(x)); convert(Interval{T}, x))
convert(::Type{Interval}, x::Interval) = x
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not directly related to the issue, but this definition is the same as the one on line 20 and should probably be deleted (especially since the result would not be correct if for some weird reason it ever gets called).

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry, I don't understand which definition you're talking about that is the same as the one on line 20?

Copy link
Collaborator

Choose a reason for hiding this comment

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

On line 23:

convert(::Type{Interval}, x::Interval) = x

Since Interval is the same as Interval{T} where {T} and nothing there requests both parameter to be the same.

Copy link
Contributor

@schillic schillic left a comment

Choose a reason for hiding this comment

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

The two changes I marked also work, and they seem to be the better solution according to Jeff's comment.

@@ -17,7 +17,7 @@ convert(::Type{Interval{T}}, x::Bool) where {T} = convert(Interval{T}, Int(x))
convert(::Type{Interval{T}}, x::Real) where {T} = atomic(Interval{T}, x)
convert(::Type{Interval{T}}, x::T) where {T<:Real} = Interval{T}(x)
convert(::Type{Interval{T}}, x::Interval{T}) where {T} = x
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
convert(::Type{Interval{T}}, x::Interval{T}) where {T} = x
convert(::Type{Interval{T}}, x::Interval{T}) where {T<:Real} = x

Copy link
Collaborator

Choose a reason for hiding this comment

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

While I see your point, this would make the code harder to maintain if we change the parameter type constrain.

Copy link
Contributor

Choose a reason for hiding this comment

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

You can add a constant and use that instead of Real.

const IT = Real  # type constraint of intervals

@@ -17,7 +17,7 @@ convert(::Type{Interval{T}}, x::Bool) where {T} = convert(Interval{T}, Int(x))
convert(::Type{Interval{T}}, x::Real) where {T} = atomic(Interval{T}, x)
convert(::Type{Interval{T}}, x::T) where {T<:Real} = Interval{T}(x)
convert(::Type{Interval{T}}, x::Interval{T}) where {T} = x
convert(::Type{Interval{T}}, x::Interval) where {T} = atomic(Interval{T}, x)
convert(::Type{Interval{T}}, x::Interval{S}) where {T,S} = atomic(Interval{T}, x)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
convert(::Type{Interval{T}}, x::Interval{S}) where {T,S} = atomic(Interval{T}, x)
convert(::Type{Interval{T}}, x::Interval) where {T<:Real} = atomic(Interval{T}, x)

@test m.sig == Tuple{typeof(convert),Type{Interval{T}},Interval{T}} where T

x = interval(1//1, 5//3)
@test convert(typeof(x), x) == x
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
@test convert(typeof(x), x) == x
@test convert(typeof(x), x) === x

This would check that it does not create a new interval.

@lbenet
Copy link
Member

lbenet commented Aug 14, 2019

Tests are not passing (in Julia >= 1.2) due to InteractiveUtils.jl; it seems to be somehow required but we don't have in Project.toml.

Aside from this, I also interpreted Jeff's remark in JuliaLang/julia#32884 as restricting the parameter T, as @schillic suggests.

@Kolaru
Copy link
Collaborator

Kolaru commented Aug 15, 2019

I see your point and wouldn't mind if it wasn't for #185: rewriting the type constrain here prevent the use of traits as a custom type allowed by a user would fail here.

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.

5 participants