在类型级别上,我偶然发现以下内容:
sealed abstract class StSource[A] {
type S
def init: S // create the initial state
def emit(s: S): (A, S) // emit a value, and update state
}
object StSource {
type Aux[A, S0] = StSource[A] {type S = S0}
def apply[A, S0](i: S0)(f: S0 => (A, S0)): Aux[A, S0] =
new StSource[A] {
type S = S0
def init = i
def emit(s: S0) = f(s)
}
}
令我感兴趣的那条线是type Aux[A, S0] = StSource[A] {type S = S0}
尤其{type S = S0}
in StSource[A] {type S = S0}
我真的不知道如何阅读这篇文章,就像解释这里涉及的结构一样。
What is StSource[A] {type S = S0}
???是一种结构类型(部分看起来像这样)
当定义像特征或类这样的类型时,类的主体是类本身表示的类型构造函数的一部分吗?其中的方法发生了什么?
真的很困惑。有人可以解构吗?
StSource[A] {type S = S0}
属于精致型。{type S = S0}
是一种类型细化。
从一方面来看,StSource[A] {type S = S0}
是一个子类型StSource[A]
.
从另一边看,StSource[A]
也是一种存在主义类型StSource[A] {type S = S0}
,即StSource[A]
is StSource.Aux[A, _]
(aka StSource.Aux[A, X] forSome {type X}
).
def test[A, S] = {
implicitly[StSource.Aux[A, S] <:< StSource[A]]
implicitly[StSource.Aux[A, _] =:= StSource[A]]
implicitly[StSource[A] =:= StSource.Aux[A, _]]
}
https://scala-lang.org/files/archive/spec/2.13/03-types.html#compound-types
A 复合型
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)