Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default implementation of AnyRef.eq method

Tags:

scala

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.

like image 735
Mohan Sundaram Avatar asked Feb 08 '23 20:02

Mohan Sundaram


1 Answers

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.).

like image 196
sjrd Avatar answered Feb 16 '23 19:02

sjrd