I am newbie to Scala and exploring the Scala source code to get the concrete understanding. When I refer the scala.AnyRef
class to understand the implementation of the eq
method, I can see the below definition and it is confusing:
final def eq(that: AnyRef): Boolean = sys.error("eq")
Since this methods throws an error: How and what is the logic eq
follows to check that two objects are same? Where can we see that implementation?
The same question applies for hashCode()
method.
And why are Any.scala
and AnyRef.scala
not part of Scala package? I can see only AnyValues.scala
.
This implementation is not really used; it's a stub. The compiler replaces calls to this method by logic of its own. The actual implementation is therefore buried deep inside the compiler.
Any.scala
and AnyRef.scala
do not have a physical existence either for the same reasons. They are magically materialized by the compiler.
In general, you cannot gain much knowledge from the source code of the primitive types (other examples: Int
, Nothing
, etc.).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With