I am converting a large code which is concatenating string and enum values. The code was using Int
as the enum type and I want to convert it to proper enums (preferably extends Enumeration
, but trait
+ objects
would do as well). I want the compiler either to perform the conversion from enum to a numeric value, or at least to give me a compiler error whenever I attempt to add an enum value to a string.
In spite of my effort following code still prints DEPTH=D0
because of the annoying default Scala String operator +:
import scala.language.implicitConversions
object Main extends App {
implicit class StringPlus(s: String) {
def + (v: Enumeration#Value): String = s + v.id.toString
}
implicit def str(v: Enumeration#Value): String = v.id.toString
val b = true
object D extends Enumeration {
val D0 = Value(100)
}
val d = D.D0
val s = "DEPTH=" + d
println(s)
}
Is there a way to disable the default string + so that the situation above is handled or detected by the compiler? I am currently using Scala 2.12, but I can port to 2.13 if necessary.