本文介绍了为什么存在`ScalaObject`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么所有Scala类都继承自 ScalaObject ,尽管该特征完全为空且与 AnyRef ,它确实定义了其他方法?

Why do all Scala classes inherit from ScalaObject although that trait is completely empty and has no (visible?) functionality compared to AnyRef, which does define additional methods?

这不会减慢方法调用,如 equals() hashCode()因为它需要考虑另一个类(可能会覆盖这些方法)?

Won't that slow down method calls like equals() or hashCode() because it will need to take another class into consideration (which might override the methods)?

是不是可以将AnyRef和ScalaObject折叠成一个类?

Isn't it possible to fold AnyRef and ScalaObject into one class?

更新:
ScalaObject ,用于优化匹配案例类构造函数的模式匹配表达式。由于名称以 $ 开头,因此它当然应被视为应用程序员隐藏。在Scala 2.8中,它完全是空的,所以我猜它是为了向后兼容。

ScalaObject inserts a $tag method, which, according to the comment in the library source code for 2.7, "is needed for optimizing pattern matching expressions which match on constructors of case classes." Since the name starts with $, it should of course be considered "hidden" to application programmers. In Scala 2.8, it's entirely empty, so I guess it's there for backward compatibility.

这篇关于为什么存在`ScalaObject`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 19:23