Closed
Description
Porting spire shows a change (Couldn't find any mention to this in the docs) from Scala 2.13 and 3.0 on operator overloading. This is repeated across the codebase which contains overloads to common operators like +
and -
. The code below is a minimization that compiles in 2.13 but fails in 3.0
A repo with the issue can be found at
https://github.com/cquiroz/implicit-operator
Compiler version
Scala 2.13 and 3.0.0.
Minimized code
import algebra.ring.AdditiveSemigroup
import algebra.ring.CommutativeSemiring
import algebra.Semigroup
import algebra.Monoid
final class AdditiveSemigroupOps[A](lhs: A)(implicit as: AdditiveSemigroup[A]) {
def +(rhs: A): A = as.plus(rhs, lhs)
def ^(rhs: A): A = as.plus(rhs, lhs)
}
trait AdditiveSemigroupSyntax {
implicit def additiveSemigroupOps[A: AdditiveSemigroup](
a: A
): AdditiveSemigroupOps[A] = new AdditiveSemigroupOps(a)
}
object syntax {
object additiveSemigroup extends AdditiveSemigroupSyntax
}
object App {
def main(args: Array[String]): Unit = {
import syntax.additiveSemigroup._
implicit def IntAlgebra[A]: AdditiveSemigroup[Map[Int, A]] = ???
def res[A]: Map[Int, A] = {
val a: Map[Int, A] = Map.empty
val b: Map[Int, A] = Map.empty
// Calls the operator on AdditiveSemigroupOps
a ^ b
// Calls the operator + on AdditiveSemigroupOps only in Scala 2
// In Scala 3 tries to call `+` on Map
a + b
}
}
}
Output
// Only in Scala 3.0
[error] -- [E007] Type Mismatch Error: /Users/cquiroz/Projects/implicit-operator/src/main/scala/Main.scala:33:10
[error] 33 | a + b
[error] | ^
[error] | Found: (b : Map[Int, A])
[error] | Required: (Int, A)
Expectation
To cross compile or have some way to override de +
operator like with the ^
operator.