@@ -710,6 +710,11 @@ const ScalarQuadraticLike{T} = Union{ScalarAffineLike{T}, MOI.ScalarQuadraticFun
710710# avoid overloading e.g. `+(::Float64, ::Float64)`
711711const ScalarLike{T} = Union{MOI. SingleVariable, MOI. ScalarAffineFunction{T},
712712 MOI. ScalarQuadraticFunction{T}}
713+ # `ScalarLike` for which `T` is defined to avoid defining, e.g.,
714+ # `+(::SingleVariable, ::Any)` which should rather be
715+ # `+(::SingleVariable, ::Number)`.
716+ const TypedScalarLike{T} = Union{MOI. ScalarAffineFunction{T},
717+ MOI. ScalarQuadraticFunction{T}}
713718
714719# Functions convertible to a VectorAffineFunction
715720const VectorAffineLike{T} = Union{Vector{T}, MOI. VectorOfVariables, MOI. VectorAffineFunction{T}}
@@ -905,21 +910,34 @@ end
905910function Base.:+ (arg:: ScalarLike{T} , args:: ScalarLike{T} ...) where T
906911 return operate (+ , T, arg, args... )
907912end
908- function Base.:+ (α:: T , arg:: ScalarLike {T} , args:: ScalarLike{T} ...) where T
913+ function Base.:+ (α:: T , arg:: TypedScalarLike {T} , args:: ScalarLike{T} ...) where T
909914 return operate (+ , T, α, arg, args... )
910915end
911- function Base.:+ (f:: ScalarLike{T} , α:: T ) where T
916+ function Base.:+ (α:: Number , f:: MOI.SingleVariable )
917+ return operate (+ , typeof (α), α, f)
918+ end
919+ function Base.:+ (f:: TypedScalarLike{T} , α:: T ) where T
912920 return operate (+ , T, f, α)
913921end
922+ function Base.:+ (f:: MOI.SingleVariable , α:: Number )
923+ return operate (+ , typeof (α), f, α)
924+ end
914925function Base.:- (arg:: ScalarLike{T} , args:: ScalarLike{T} ...) where T
915926 return operate (- , T, arg, args... )
916927end
917- function Base.:- (f:: ScalarLike {T} , α:: T ) where T
928+ function Base.:- (f:: TypedScalarLike {T} , α:: T ) where T
918929 return operate (- , T, f, α)
919930end
920- function Base.:- (α:: T , f:: ScalarLike{T} ) where T
931+ function Base.:- (f:: MOI.SingleVariable , α:: Number )
932+ return operate (- , typeof (α), f, α)
933+ end
934+ function Base.:- (α:: T , f:: TypedScalarLike{T} ) where T
921935 return operate (- , T, α, f)
922936end
937+ function Base.:- (α:: Number , f:: MOI.SingleVariable )
938+ return operate (- , typeof (α), α, f)
939+ end
940+
923941
924942# Vector +/-
925943# ##############################################################################
@@ -1150,7 +1168,10 @@ function operate!(::typeof(*), ::Type{T}, f::MOI.SingleVariable, α::T) where T
11501168 return operate (* , T, α, f)
11511169end
11521170function operate (:: typeof (* ), :: Type{T} , α:: T , f:: MOI.SingleVariable ) where T
1153- MOI. ScalarAffineFunction {T} ([MOI. ScalarAffineTerm (α, f. variable)], zero (T))
1171+ return MOI. ScalarAffineFunction {T} ([MOI. ScalarAffineTerm (α, f. variable)], zero (T))
1172+ end
1173+ function operate (:: typeof (* ), :: Type{T} , f:: MOI.SingleVariable , α:: T ) where T
1174+ return operate (* , T, α, f)
11541175end
11551176
11561177function operate! (:: typeof (* ), :: Type{T} ,
@@ -1223,15 +1244,23 @@ function operate(::typeof(*), ::Type{T}, f::MOI.ScalarAffineFunction{T},
12231244 return MOI. ScalarQuadraticFunction (aff_terms, quad_terms, constant)
12241245end
12251246
1226- function Base.:* (args:: ScalarLike{T} ...) where T
1227- return operate (* , T, args... )
1247+ # To avoid type piracy, we add at least one `ScalarLike` outside of the `...`.
1248+ function Base.:* (arg:: ScalarLike{T} , args:: ScalarLike{T} ...) where T
1249+ return operate (* , T, arg, args... )
12281250end
1229- function Base.:* (f:: T , g:: ScalarLike {T} ) where T
1251+ function Base.:* (f:: T , g:: TypedScalarLike {T} ) where T
12301252 return operate (* , T, f, g)
12311253end
1232- function Base.:* (f:: ScalarLike{T} , g:: T ) where T
1254+ function Base.:* (f:: Number , g:: MOI.SingleVariable )
1255+ return operate (* , typeof (f), f, g)
1256+ end
1257+ function Base.:* (f:: TypedScalarLike{T} , g:: T ) where T
12331258 return operate (* , T, g, f)
12341259end
1260+ function Base.:* (f:: MOI.SingleVariable , g:: Number )
1261+ return operate (* , typeof (g), f, g)
1262+ end
1263+
12351264
12361265# ###################################### / ######################################
12371266function promote_operation (:: typeof (/ ), :: Type{T} ,
@@ -1278,12 +1307,12 @@ function operate(::typeof(/), ::Type{T},
12781307 return operate! (/ , T, copy (f), α)
12791308end
12801309
1281- function Base.:/ (args:: ScalarLike{T} ...) where T
1282- return operate (/ , T, args... )
1283- end
1284- function Base.:/ (f:: ScalarLike{T} , g:: T ) where T
1310+ function Base.:/ (f:: TypedScalarLike{T} , g:: T ) where T
12851311 return operate (/ , T, f, g)
12861312end
1313+ function Base.:/ (f:: MOI.SingleVariable , g:: Number )
1314+ return operate (/ , typeof (g), f, g)
1315+ end
12871316
12881317# # sum
12891318function operate (:: typeof (sum), :: Type{T} , vis:: Vector{MOI.VariableIndex} ) where T
0 commit comments