From 2dbcf3c82d61c1d5962d94bb69cca0e6f9f41229 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20G=C3=B6ttgens?= Date: Tue, 11 Jun 2024 23:02:45 +0200 Subject: [PATCH] Remove overloading `promote_type` (#426) * Remove overloading `promote_type` * Add arithmetics for CxxNumber Co-authored-by: Max Horn * Remove automatic coercion of arith results to keep this change non-breaking --------- Co-authored-by: Max Horn --- src/CxxWrap.jl | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/CxxWrap.jl b/src/CxxWrap.jl index 4640b96..1046466 100644 --- a/src/CxxWrap.jl +++ b/src/CxxWrap.jl @@ -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)) @@ -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