问题描述
为什么所有Scala类都继承自 ScalaObject
,尽管该特征完全为空且与 AnyRef $相比没有(可见?)功能c $ c>,它确实定义了其他方法?
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`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!