First, to why you're seeing a MatchError
. A value on an object (A.f) is considered a stable identifier (as Scala reference says, "Stable members are [...] members introduced by object definitions or by value definitions of non-volatile types").
Here's what the typer output looks like:
object A extends scala.AnyRef {
...
private[this] val f: String = "Object A";
<stable> <accessor> def f: String = A.this.f
}
When used in an assignment, the compiler "desugars" assignment of this stable identifier (it being stable is a necessary condition) into pattern matching:
<synthetic> private[this] val x$1: Unit = ("Object B": String("Object B") @unchecked) match {
case A.f => ()
}
It can't match "Object B" against a pattern "Object A", so it throws a MatchError
.
To your larger question: you can't/shouldn't override values and methods on a companion object. Polymorphism applies to classes and their instances, not to static methods or values. There's probably a better way to think about your program that doesn't involve overriding vals/defs on a companion object.