Scala的静态重载规则是什么? [英] What is Scala's static overloading rule?

查看:128
本文介绍了Scala的静态重载规则是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在许多关于Scala隐式优先级的解释中,它指出,如果有多个事物具有相同的优先级,则会应用Scala的静态重载规则".

In many explanations about Scala's implicit precedence, it states that if there is more than one thing with the same precedence, Scala's "static overloading rule" is applied.

但是没有解释该规则.似乎此表达式仅在此上下文中使用. Scala的静态重载规则是什么?

That rule isn't explained, though. It seems that this expression is used exclusively in this context. What is Scala's static overloading rule?

推荐答案

这在此答案中所述,有一个

This is explained in §6.26.3 of the Scala Language Specification. As also noted in this answer, there is a blog post that lists this resolution in a simpler way:

替代项A相对于替代项B的相对权重是一个从0到2的数字,定义为

The relative weight of an alternative A over an alternative B is a number from 0 to 2, defined as the sum of

  • 如果A与B一样具体,则为1,否则为0,
  • 如果A是在从定义B的类或对象派生的类或对象中定义的,则为1,否则为0.
  • 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.

如果满足以下条件之一,则从类或对象D派生类或对象C:

A class or object C is derived from a class or object D if one of the following holds:

  • C是D的子类,或者
  • C是从D派生的类的伴侣对象,或者
  • D是派生C的类的伴随对象.
  • 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.

如果A对B的相对权重大于B对A的相对权重,则替代A比替代B更具体.

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.

For views, if A is as specific view as B, A gets a relative weight of 1 over B.

如果在定义了B的派生类中定义A,则A将获得另一个相对权重.

If A is defined in a derived class in which B is defined, A gets another relative weight.

这篇关于Scala的静态重载规则是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆