通常,我用<:表示A <:B之类的子类型关系,作为类型参数的一部分或类型成员。在浏览某些内容时,我遇到了这种“ <:<”表示形式。在Predef.scala中找到它,令人惊讶的是,它被定义为抽象类。Doc说:
是的子类型的
A <:< B见证人实例。要求隐式类型的参数编码广义约束。ABA <:< BA <: B
考虑到两者都表示相同的“子类型”关系(AFAIK),请问一下两者之间到底有什么区别。另外,请提出正确的用法(我的意思是,其中<:<比<:更可取)?
[A <: B]声明A具有已知属性/限制的类型参数:它必须是类型B(现有类型)或其子类型。
class A // A and B are unrelated
class B
// these both compile
def f1[A <: B]() = ??? // A is the type parameter, not a reference to class A
def f2[B <: A]() = ??? // B is the type parameter, not a reference to class B
Run Code Online (Sandbox Code Playgroud)
[A <:< B] 用于测试现有类型。
class B
class A extends B
// A and B must already exist and have this relationship or this won't compile
implicitly[A <:< B]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
85 次 |
| 最近记录: |