简体   繁体   English

Scala的类层次结构

[英]Scala's class hierarchy

Questions on Scala's class hierarchy: 关于Scala类层次结构的问题:

  1. In the IntelliJ IDE, I am looking for the implementation of Any and AnyRef , but they are not there. 在IntelliJ IDE中,我正在寻找AnyAnyRef的实现,但它们并不存在。 Where are they defined and how can I see their code? 他们在哪里定义,我怎么能看到他们的代码?

  2. I read the following in "Programming in Scala" 我在“Scala编程”中阅读了以下内容

The only case where == does not directly call equals is for Java's boxed numeric classes, such as Integer or Long. ==不直接调用equals的唯一情况是Java的盒装数字类,例如Integer或Long。 In Java, a new Integer(1) does not equal a new Long(1) even though for primitive values 1 == 1L. 在Java中,即使原始值1 == 1L,新的Integer(1)也不等于新的Long(1)。 Since Scala is a more regular language than Java, it was necessary to correct this discrepancy by special-casing the == method for these classes. 由于Scala是一种比Java更常规的语言,因此有必要通过对这些类特殊地设置==方法来纠正这种差异。

Hmm... but isn't == final? 嗯......但不是==最终? How did they make a special case for Java's numeric classes? 他们是如何为Java的数字类做出特殊情况的?

Any , AnyRef , Null , and Nothing do not actually exist; AnyAnyRefNullNothing实际上并不存在; 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的存储库中找到一个引用 ,但正如它所说,这些引用实际上并不存在,仅用于文档和引导目的。 Scala's 3 Any* s are actually all java.lang.Object in the compiled bytecode. Scala的3 Any* s实际上是编译后的字节码中的所有java.lang.Object

Additionally, the primitive value classes ( Int , Long , etc.) are all defined final abstract , meaning that they also do not actually exist. 另外,原始值类( IntLong等)都是定义的final abstract ,这意味着它们实际上也不存在。 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. 这是允许==进行空值检查并正确处理值类型的原因,即使它是final ,因为编译器正在围绕它进行魔术。 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)) . new java.lang.Integer(5) == new java.lang.Long(5) ,它归结为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 . 一个明显的例子:你可以声明一个超类,或者你不能声明一个超类,在这种情况下,超类是AnyRef So, how do you write Any , a class that has no superclass? 那么,你如何编写Any ,一个没有超类的类? 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. 您可以扩展语言并允许程序员编写没有超类的类,但是您不能再保证Any唯一的类,因此您的继承层次结构将不再形成晶格。 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. 因此,唯一合理的选择是对Any的定义实际上不能用语言本身表达。

This is not peculiar to Scala: you have the same problem with Java's and Ruby's Object , for example. 这不是Scala所特有的:例如,你和Java和Ruby的Object有同样的问题。

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.). 事实上,Scala有了很多这样的“神奇”相比其他主流语言......和“魔术师”它确实有一个显著部分是与底层主机的生态系统(Java的斯卡拉,JVM,ECMAScript的用于Scala.js平台兼容性,用于废弃的Scala.NET的CLI,用于假设的Scala-macOS的CoreFoundation等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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