在 Scala 中覆盖具有 Path Dependent 类型的成员.需要根据 Scala 语言规范进行解释 [英] Overriding members having Path Dependent types in Scala. Explanation is needed in terms of Scala Language Specification

查看:62
本文介绍了在 Scala 中覆盖具有 Path Dependent 类型的成员.需要根据 Scala 语言规范进行解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下简单的 Scala 实验:

Consider the following simple Scala experiment:

scala> trait A {class C;val c:C}
defined trait A

scala> object O1 extends A {val c=new C}
defined object O1

scala> object O2 extends A {val c=O1.c}
<console>:9: error: overriding value c in trait A of type O2.C;
 value c has incompatible type
       object O2 extends A {val c=O1.c}

根据 Scala 语言规范 (SLS 5.1.4):

According to the Scala Language Specification (SLS 5.1.4):

O1.c的类型应该符合A.c的类型,因为前者覆盖了后者.

The type of the value O1.c should conform to the type of A.c because the former overrides the latter.

问题 1:

在 SLS 中的何处指定了O1.c 的类型确实符合 A.c 的类型的规则?

Where in the SLS it is specified the rule from which it follows that the type of O1.c indeed does conform to the type of A.c?

那么为什么object O1 extends A {val c=new C} 编译根据SLS?

问题 2:

在 SLS 中的何处指定了它遵循的规则,即值 O1.c 的类型不符合值 O2.c 的类型>?

Where in the SLS it is specified the rule from which it follows that the type of the value O1.c does not conform to the type of the value O2.c?

换句话说,它遵循哪个 SLS 规则object O2 extends A {val c=O1.c} 不应该编译?

In other words, from which SLS rule it follows that object O2 extends A {val c=O1.c} should not compile?

相关:根据SLS,为什么O2.c的值的类型应该是O2.C?

Related : why should the type of the value of O2.cbe O2.C according to the SLS ?

更新:

根据SLS,值O1.c和抽象值成员A.c的类型是什么?

What is the type of the value O1.c and of the abstract value member A.c according to the SLS ?

推荐答案

Ac的类型为A#CO1.c>(更新:O1.C 类型,根据 SLS 是 O1.type#C)确实符合 A#c.

The type of A.c is A#C, and O1.c (update: of type O1.C, which per the SLS is O1.type#C) does indeed conform to A#c.

问题1:在O1里面,C的类型是O1.Cnew C>new O1.C(参见 3.2.3 中类型指示符的规则).O1.C 符合 O1.C 因为它是等价的(因为等价是一个同余).

Question 1: inside O1, the type C is O1.C and new C is new O1.C (see the rules for type designators in 3.2.3). O1.C conforms to O1.C because it is equivalent (because equivalence is a congruence).

问题2:因为O1不符合O2,所以3.5.1或3.5.2中的任何规则都不会使O1 符合 O2.因此 3.5.2 中的类型投影规则不适用.

Question 2: It follows because O1 does not conform to O2, because none of the rules in 3.5.1 or 3.5.2 would make O1 conform to O2. So the rule for type projections in 3.5.2 doesn't apply.

根据 5.1.1,构造函数调用返回该类型的值.

A constructor invocation returns a value of that type, per 5.1.1.

(我真的不认为规范是学习这些东西的最佳方式;最好先对事物的工作原理有一个实际的了解.

(I really don't think the specification is the best way to learn these things; better to get a practical sense of how things work first).

这篇关于在 Scala 中覆盖具有 Path Dependent 类型的成员.需要根据 Scala 语言规范进行解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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