微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么可以用未定义路径依赖的类型定义一个类?

如何解决为什么可以用未定义路径依赖的类型定义一个类?

考虑以下示例:

trait Supe {

  type Out <: Supe
  def out: Out
}

class Reif1 extends Supe {

  type Out = Reif1
  override def out: Out = this
}

class Reif2 extends Supe {

  type Out >: this.type <: Reif2
  override def out: Out = this
}

class Reif1显然应该可以工作。 type Out被改写并成为类型别名

class Reif2也可以,但是认真吗? type Out仅定义了上限/下限,并且边界还不够严格:this.type是单例类型,而Reif2是类类型。因此,如果实例化OutReif2将会是什么样?是this.type吗?还是Reif2?但是更大的问题应该是:为什么scalac 2.12 / 2.13允许对其进行编译?

解决方法

在Scala(或DOT演算sorted() 1)中,所有类型都是区间。

type Out = Reif1是(或应该是)type Out >: Reif1 <: Reif1

不受限制的抽象类型type Outtype Out >: Nothing <: Any

如果Out实例化,Reif2到底会是什么样?

它将完全保持type Out >: this.type <: Reif2

val r = new Reif2

import scala.reflect.runtime.universe._
typeOf[r.Out] // App.r.Out
showRaw(typeOf[r.Out]) // TypeRef(SingleType(ThisType(App),TermName("r")),TypeName("Out"),List())
typeOf[r.Out].typeSymbol.isAbstract // true
typeOf[r.Out].typeSymbol.typeSignature //  >: Reif2.this.type <: App.Reif2

如果将type Out >: this.type <: Reif2中的Reif2替换为type Out = this.type(或type Out = Reif2type Out = Supe),则isAbstract将返回false

2(请参见抽象类型的应用)

Abstract type member of a singleton object(请参阅为什么不容易检查type T >: L <: U不是抽象的东西)

What is the meaning of a type declaration without definition in an object?

Concrete classes can have abstract type members #1753

SI-8217 allow abstract type members in objects #4024

Abstract type members are incorrectly forbidden in objects (unless inherited) #8217

Use of abstract type in a concrete class?

Concrete classes with abstract type members

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。