Search code examples
scalacompatibility

Can a method really be binary compatible with a class-object (scala)?


The metrics-scala project got a pull-request that suggests to change:

class Meter {
  def exceptionMarker: new AnyRef() {
    def apply[A](f: => A): A = ???
  }
}

to the much nicer:

class Meter {
  object exceptionMarker {    // only this line changed
    def apply[A](f: => A): A = ???
  }
}

However, I am afraid this causes binary incompatibility. Of course I checked with Mima. Mima does not complain.

So my question is: is Mima right, and is the proposed change really binary compatible?


Solution

  • In fact you can even implement/override a def with an object in a subclass.

    trait Foo
    trait A {
      def foo: Foo
    }
    class B extends A {
      object foo extends Foo
    }
    

    So yes, I am fairly sure it should be backwards binary compatible.