加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

为什么Scala中具有不同组件顺序的复合类型是等效的?

发布时间:2020-12-16 18:48:53 所属栏目:安全 来源:网络整理
导读:根据 Scala Language Specification, Two compound types are equivalent if the sequences of their component are pairwise equivalent,and occur in the same order,and their refinements are equivalent. Two refinements are equivalent if they bind
根据 Scala Language Specification,

Two compound types are equivalent if the sequences of their component are pairwise equivalent,and occur in the same order,and their refinements are equivalent. Two refinements are equivalent if they bind the same names and the modifiers,types and bounds of every declared entity are equivalent in both refinements.

但是,给定

trait A { val a: Int }
trait B { val b: String }

我越来越

scala> implicitly[A with B =:= B with A]
res0: =:=[A with B,B with A] = <function1>

即使组件的顺序不同,它们也被认为是等效的.为什么?

解决方法

我认为=:=证据只断言每个都是另一个的上限.

trait A; trait B

import scala.reflect.runtime.{universe => ru}

val ab = ru.typeOf[A with B]
val ba = ru.typeOf[B with A]
ab =:= ba   // false!
ab <:< ba   // true!
ba <:< ab   // true!

如果LUB(X,Y)== X == Y,则从Predef隐式得到,因为隐式分辨率找到具有推断上限的=:=.tpEquals.

这很可能是你想要的,因为它意味着你可以将一种类型视为另一种类型,这是有效的,因为A的成员与B的成员相等,即使实现中的特征线性化不同.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读