Search code examples
javainheritancescalaprogramming-languageslanguage-design

Why does `ScalaObject` exist?


Why do all Scala classes inherit from ScalaObject although that trait is completely empty and has no (visible?) functionality compared to AnyRef, which does define additional methods?

Won't that slow down method calls like equals() or hashCode() because it will need to take another class into consideration (which might override the methods)?

Isn't it possible to fold AnyRef and ScalaObject into one class?

UPDATE: ScalaObject was eradicated with new 2.10 version of Scala.


Solution

  • ScalaObject inserts a $tag method, which, according to the comment in the library source code for 2.7, "is needed for optimizing pattern matching expressions which match on constructors of case classes." Since the name starts with $, it should of course be considered "hidden" to application programmers. In Scala 2.8, it's entirely empty, so I guess it's there for backward compatibility.