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

scala – Set中&function的奇怪行为

发布时间:2020-12-16 09:00:34 所属栏目:安全 来源:网络整理
导读:Set定义为Set [A].它需要一个变量参数.由于我们正在传递共变量参数,因此按照预期工作: scala val a = Set(new Object)a: scala.collection.immutable.Set[Object] = Set(java.lang.Object@118c38f)scala val b = Set("hi")b: scala.collection.immutable.Se
Set定义为Set [A].它需要一个变量参数.由于我们正在传递共变量参数,因此按照预期工作:

scala> val a = Set(new Object)
a: scala.collection.immutable.Set[Object] = Set(java.lang.Object@118c38f)

scala> val b = Set("hi")
b: scala.collection.immutable.Set[String] = Set(hi)

scala> a & b
<console>:10: error: type mismatch;
 found   : scala.collection.immutable.Set[String]
 required: scala.collection.GenSet[Object]
Note: String <: Object,but trait GenSet is invariant in type A.
You may wish to investigate a wildcard type such as `_ <: Object`. (SLS 3.2.10)
              a & b

但以下工作:

scala> Set(new Object) & Set("hi")
res1: scala.collection.immutable.Set[Object] = Set()

在我看来,scala编译器将Set(“hi”)转换为Set [Object]类型,因此可以工作.

在这里做什么类型推理?有人可以链接到解释行为的规范,以及何时发生?不应该为这种情况抛出编译时错误吗?作为相同操作类型的2个不同输出.

解决方法

不确定,但我认为您正在寻找的内容在 the language spec“本地类型推断”(在本文撰写时,第100页第6.26.4节)中有所描述.

Local type inference infers type arguments to be passed to expressions of polymorphic type. Say e is of type [ a1 >: L1 <: U1,…,an >: Ln <: Un ] T and no explicit type
parameters are given.

Local type inference converts this expression to a type application e [ T1,Tn ]. The choice of the type arguments T1,Tn depends on the context in which the expression appears and on the expected type pt. There are three cases.

[ … ]

If the expression e appears as a value without being applied to value arguments,the type arguments are inferred by solving a constraint system which relates the expression’s type T with the expected type pt. Without loss of generality we can assume that T is a value type; if it is a method type we apply eta-expansion to convert it to a function type. Solving means ?nding a substitution σ of types Ti for the type parameters ai such that

  • None of inferred types Ti is a singleton type

  • All type parameter bounds are respected,i.e. σ Li <: σ ai and σ ai <: σ Ui for i = 1,n.

  • The expression’s type conforms to the expected type,i.e. σ T <: σ pt.

It is a compile time error if no such substitution exists. If several substitutions exist,local-type inference will choose for each type variable ai a minimal or maximal type Ti of the solution space. A maximal type Ti will be chosen if the type parameter ai appears contravariantly in the type T of the expression. A minimal type Ti will be chosen in all other situations,i.e. if the variable appears covariantly,nonvariantly or not at all in the type T. We call such a substitution an optimal solution of the given constraint system for the type T.

简而言之:Scalac必须为您省略的泛型类型选择值,并在结果编译的约束下选择可能的最具体选择.

(编辑:李大同)

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

    推荐文章
      热点阅读