scala – 为什么要通过@uV禁用方差检查?
发布时间:2020-12-16 08:47:35 所属栏目:安全 来源:网络整理
导读:参见英文答案 When is @uncheckedVariance needed in Scala,and why is it used in GenericTraversableTemplate?????????????????????????????????????3个 我只是偶然发现了由 TraversableLike (2.10.0)定义的以下方法定义: override def to[Col[_]](implic
参见英文答案 >
When is @uncheckedVariance needed in Scala,and why is it used in GenericTraversableTemplate?????????????????????????????????????3个
我只是偶然发现了由 TraversableLike (2.10.0)定义的以下方法定义:
override def to[Col[_]](implicit cbf: CanBuildFrom[Nothing,A,Col[A @uV]]): Col[A @uV] = { val b = cbf() b.sizeHint(this) b ++= thisCollection b.result } 根据this answer,@ uV禁用方差检查.这听起来很危险.我为什么要那样做? 解决方法
我猜答案与这个问题的答案非常相似:
When is @uncheckedVariance needed in Scala,and why is it used in GenericTraversableTemplate?
由于构造函数(隐式参数cbf)存在于不可变(可能是协变)和可变(不变)集合中,因此这里可能使用注释使to方法适用于两种类型的集合. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |