简体   繁体   中英

Scala's class hierarchy

Questions on Scala's class hierarchy:

  1. In the IntelliJ IDE, I am looking for the implementation of Any and AnyRef , but they are not there. Where are they defined and how can I see their code?

  2. I read the following in "Programming in Scala"

The only case where == does not directly call equals is for Java's boxed numeric classes, such as Integer or Long. In Java, a new Integer(1) does not equal a new Long(1) even though for primitive values 1 == 1L. Since Scala is a more regular language than Java, it was necessary to correct this discrepancy by special-casing the == method for these classes.

Hmm... but isn't == final? How did they make a special case for Java's numeric classes?

Any , AnyRef , Null , and Nothing do not actually exist; they are included to make the type system complete, but they have no real representation. You can find a reference in Scala's repository , but as it says, these do not actually compile and exist for documentation and bootstrapping purposes only. Scala's 3 Any* s are actually all java.lang.Object in the compiled bytecode.

Additionally, the primitive value classes ( Int , Long , etc.) are all defined final abstract , meaning that they also do not actually exist. All of their methods are abstract.

In order to make everything look neat and tidy, and more importantly, work, it is the compiler's job to fake the existence of these types and perform magic to make it all stick together. This is what allows == to do null-checking and properly handle value types even though it's final , as the compiler is doing magic around it. In the case of new java.lang.Integer(5) == new java.lang.Long(5) , it boils down to scala.runtime.BoxesRuntime.equalsNumNum(new java.lang.Integer(5), new java.lang.Long(5)) .

It is quite normal for languages to have "magic" parts like this. Parts that cannot be described in the language itself.

The obvious example: you can either declare a superclass, or you can not declare a superclass, in which case the superclass is AnyRef . So, how do you write Any , a class that has no superclass? Well, you can't. You could extend the language and allow programmers to write classes with no superclasses, but then you could no longer guarantee that Any is the only such class, and thus your inheritance hierarchy would no longer form a lattice. But, the type checking and type inferencing rules depend on the inheritance hierarchy being a lattice.

So, the only sane choice is to have a definition of Any that cannot actually be expressed in the language itself.

This is not peculiar to Scala: you have the same problem with Java's and Ruby's Object , for example.

Actually, Scala has a lot less such "magic" compared to other mainstream languages … and a significant fraction of the "magic" it does have is for platform compatibility with the underlying host ecosystem (Java for Scala-JVM, ECMAScript for Scala.js, CLI for the abandoned Scala.NET, CoreFoundation for a hypothetical Scala-macOS, etc.).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM