Skip to content

Commit

Permalink
Remove overloading promote_type (#426)
Browse files Browse the repository at this point in the history
* Remove overloading `promote_type`

* Add arithmetics for CxxNumber

Co-authored-by: Max Horn <max@quendi.de>

* Remove automatic coercion of arith results

to keep this change non-breaking

---------

Co-authored-by: Max Horn <max@quendi.de>
  • Loading branch information
lgoettgens and fingolfin committed Jun 11, 2024
1 parent 35b258f commit 2dbcf3c
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/CxxWrap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ function Base.promote_rule(::Type{CT}, ::Type{JT}) where {CT <: CxxNumber, JT <:
end
return Base.promote_rule(julia_int_type(CT), JT)
end
Base.promote_type(::Type{T}, ::Type{T}) where {T<:CxxNumber} = julia_int_type(T)
Base.promote_rule(::Type{T}, ::Type{T}) where {T<:CxxNumber} = Base.promote_type(T,T)
Base.promote_rule(::Type{T}, ::Type{T}) where {T<:CxxNumber} = julia_int_type(T)
Base.promote_rule(::Type{T1}, ::Type{T2}) where {T1<:CxxNumber, T2<:CxxNumber} = Base.promote_rule(julia_int_type(T1), julia_int_type(T2))
Base.AbstractFloat(x::CxxNumber) = Base.AbstractFloat(to_julia_int(x))

Expand All @@ -125,6 +124,15 @@ Base.Bool(x::T) where {T<:CxxNumber} = Bool(reinterpret(julia_int_type(T), x))::

Base.flipsign(x::T, y::T) where {T <: CxxSigned} = reinterpret(T, flipsign(to_julia_int(x), to_julia_int(y)))

for op in (:+, :-, :*, :&, :|, :xor)
@eval function Base.$op(a::S, b::S) where {S<:CxxNumber}
T = julia_int_type(S)
aT, bT = a % T, b % T
Base.not_sametype((a, b), (aT, bT))
return $op(aT, bT)
end
end

# Trait type to indicate a type is a C++-wrapped type
struct IsCxxType end
struct IsNormalType end
Expand Down

0 comments on commit 2dbcf3c

Please sign in to comment.