Skip to content

Commit

Permalink
Merge pull request #9 from jishnub/ambiguity
Browse files Browse the repository at this point in the history
Add methods to resolve ambiguity with Base
  • Loading branch information
sostock authored Jun 18, 2020
2 parents 4cc35b2 + 6a07724 commit 38e9cfb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/HalfIntegers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ Base.://(x::HalfInteger, y::HalfInteger) = twice(x)//twice(y)
Base.://(x::HalfInteger, y) = twice(x)//twice(y)
Base.://(x, y::HalfInteger) = twice(x)//twice(y)

# Ambiguity resolution with Base
Base.://(x::HalfInteger, y::Complex) = twice(x)//twice(y)
Base.://(x::Complex, y::HalfInteger) = twice(x)//twice(y)
Base.://(x::AbstractArray, y::HalfInteger) = twice.(x) .// twice(y)

Base.:^(x::Real, y::HalfInteger) = x^float(y)
Base.:^(::Irrational{:ℯ}, x::HalfInteger) = exp(x)

Expand Down
10 changes: 10 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ halfuinttypes = (:HalfUInt, :HalfUInt8, :HalfUInt16, :HalfUInt32, :HalfUInt64, :

==(x, y) = x === y
==(x::T, y::T) where T<:Union{BigInt,BigFloat,BigHalfInt,Rational{BigInt},Complex{BigInt},Complex{BigFloat},Complex{BigHalfInt},Complex{Rational{BigInt}}} = x == y
==(x::AbstractArray, y::AbstractArray) = (x == y) && (typeof(x) == typeof(y))

@test isempty(Test.detect_ambiguities(HalfIntegers, Base))

@testset "Constructors" begin
for T in (halfinttypes..., halfuinttypes..., :BigHalfInt)
Expand Down Expand Up @@ -671,6 +674,13 @@ end
@test 2 // BigHalfInt(2) ==Rational{BigInt}(1//1)
@test BigInt(2) // BigHalfInt(2) ==Rational{BigInt}(1//1)
@test (4//3) // BigHalfInt(3/2) ==Rational{BigInt}(8//9)

# Extra tests for ambiguity resolution with Base
for T in (halfinttypes..., halfuinttypes..., BigHalfInt)
@eval @test [1]//$T(2) ==ₜ [1//$T(2)]
@eval @test Complex(2,0)//$T(2) ==Complex(2//$T(2),0//1)
@eval @test $T(2)//Complex(2,0) ==Complex($T(2)//2,0//1)
end
end

@testset "div/rem/mod" begin
Expand Down

0 comments on commit 38e9cfb

Please sign in to comment.