Search code examples
scalatype-erasure

In Scala all abstract types have the same type after erasure


The following code does not compile, as both multiplication operators have same type after erasure: (f: Object)Object

I know about type erasure, but all cases I have seen had erased the generic type, like List[Int] or List[String], as answered in Scala double definition (2 methods have the same type erasure).

How can I make the Scala treat different type XxxT` types different?

trait AbstractTypes {
  type ScalarT
  type VectorT
  abstract class Operators(u: VectorT) {
    def *(f: ScalarT): VectorT
    def *(v: VectorT): VectorT
  }
}

Solution

  • This is what DummyImplicit is for:

    trait AbstractTypes {
      type ScalarT
      type VectorT
      abstract class Operators(u: VectorT) {
        def *(f: ScalarT): VectorT
        def *(v: VectorT)(implicit dummy1: DummyImplicit): VectorT
      }
    }
    

    You can have any number of DummyImplicits if you need more overloads whose erasure are the same.