You do not need to explicitly extend AnyRef. AnyRef is the equivalent of a Java object in Scala. By creating an instance of the class, this instance will distribute AnyRef implicitly, regardless of whether it was explicitly specified in one of its superclasses or not.
The following code just creates an anonymous class, so it also extends AnyRef implicitly:
trait A val a = new A { }
Here is an explanation from scala -lang.org:
The superclass of all Scala classes. Anyone has two direct subclasses of Scala.AnyVal and Scala.AnyRef, representing two different classes: value classes and reference classes. All value classes are predefined; they correspond to primitive types of Java-like languages. All other classes define reference types. User-defined classes define default link types; that is, always (indirectly) a subclass of Scala.AnyRef. Each custom class in Scala implicitly extends the Scala.ScalaObject trait. Classes from the infrastructure on which Scala runs (such as the Java runtime) do not extend Scala.ScalaObject. If Scala is used in the context of the Java runtime, then Scala.AnyRef corresponds to java.lang.Object. Note that the above diagram also shows implicit conversions called representations between value classes. Here is an example that demonstrates that both numbers, characters, logical values, and functions are objects, just like any other object:
http://docs.scala-lang.org/tutorials/tour/unified-types.html
Since Scala 2.10, you can also extend AnyVal. From scala -lang.org:
AnyVal is the root class of all value types that describe values ββthat are not implemented as objects in the host base system. Prior to Scala 2.10, AnyVal was a sealed tag. However, starting with Scala 2.10, you can define a subclass of AnyVal, called a custom value class, which is specially handled by the compiler. Properly defined user value classes provide a way to improve performance on user types by avoiding the placement of objects at run time and replacing virtual method calls with static method calls.
Monads are like ...
source share