问题描述
我正在恐惧地阅读Scala 3附带的内容,特别注意对化合物类型的更改。它们总是有些骇人听闻,因此干净,真实的交叉点类型无疑是一种改进。我找不到关于复合类型的实际优化部分会发生什么的任何事情。在当前的项目中,我严重依赖于紧密交织的类型,试图使每个返回值都尽可能地窄。因此,例如,拥有
trait Thing { thisThing =>
type A
type B
type C
def something :Thing {
type A = <related to thisThing.A>
type B <: <related to thisThing.B>
type C = <etc>
}
还有可能吗?如何用新的语义实现这个目标?据我了解,几乎肯定不允许对抽象类型进行细化,因此很难在类型层次结构的根中具有“自我类型”声明:
trait Thing {
type ThisThing <: Thing
type A
type B
def copy(...) :ThisThing { type A = ...; type B = ... }
}
从某种程度上讲,我正在研究结构类型。我必须说我喜欢如何使用静态声明来动态选择/实现成员。虽然抽象类有可能吗?我可以这样写吗:
trait Record(members :(String,Any)*) extends Selectable { ... }
val r = new Record { val name :String; val age :Int }
编辑:我发现文档的相对位置,并且看来匿名Selectable
实例不受该匿名类的类型是其扩展类型的交集-好的规则。
解决方法
据我了解,几乎肯定不允许对抽象类型进行细化...
type A <: {
type U
}
trait B {
type T <: A
}
object o extends B {
type T = A { type U = Int }
}
完全编译。
https://scastie.scala-lang.org/Nbz3GxoaTSe3VhXZz406vQ
不允许的是抽象类型T#U
的类型投影
trait B {
type T <: A
type V = T#U // B.this.T is not a legal path since it is not a concrete type
}
https://scastie.scala-lang.org/xPylqOOkTPK9CvWyDdLvsA
http://dotty.epfl.ch/docs/reference/dropped-features/type-projection.html
也许你是那个意思
type A {
type U
}
不可解析。但是在Scala 2中也不是。正确是
trait A {
type U
}
或
type A <: {
type U
}