Scala 的静态重载规则是什么?
What is Scala's static overloading rule?
在许多关于 Scala 隐式优先级的解释中,它声明如果有多个事物具有相同的优先级,则应用 Scala 的 "static overloading rule"。
虽然没有解释该规则。似乎这个表达专门用于这种情况。 Scala的静态重载规则是什么?
这在 §6.26.3 of the Scala Language Specification. As also noted in this answer, there is a blog post 中有解释,它以更简单的方式列出了此分辨率:
The relative weight of an alternative A over an alternative B is a number from 0 to 2, defined as the sum of
- 1 if A is as specific as B, 0 otherwise, and
- 1 if A is defined in a class or object which is derived from the class or object defining B, 0 otherwise.
A class or object C is derived from a class or object D if one of the following holds:
- C is a subclass of D, or
- C is a companion object of a class derived from D, or
- D is a companion object of a class from which C is derived.
An alternative A is more specific than an alternative B if the relative weight of A over B is greater than the relative weight of B over A.
对于视图,如果 A 是与 B 一样具体的视图,则 A 相对于 B 的相对权重为 1。
如果在定义了 B 的派生 class 中定义 A,则 A 获得另一个相对权重。
在许多关于 Scala 隐式优先级的解释中,它声明如果有多个事物具有相同的优先级,则应用 Scala 的 "static overloading rule"。
虽然没有解释该规则。似乎这个表达专门用于这种情况。 Scala的静态重载规则是什么?
这在 §6.26.3 of the Scala Language Specification. As also noted in this answer, there is a blog post 中有解释,它以更简单的方式列出了此分辨率:
The relative weight of an alternative A over an alternative B is a number from 0 to 2, defined as the sum of
- 1 if A is as specific as B, 0 otherwise, and
- 1 if A is defined in a class or object which is derived from the class or object defining B, 0 otherwise.
A class or object C is derived from a class or object D if one of the following holds:
- C is a subclass of D, or
- C is a companion object of a class derived from D, or
- D is a companion object of a class from which C is derived.
An alternative A is more specific than an alternative B if the relative weight of A over B is greater than the relative weight of B over A.
对于视图,如果 A 是与 B 一样具体的视图,则 A 相对于 B 的相对权重为 1。
如果在定义了 B 的派生 class 中定义 A,则 A 获得另一个相对权重。