简体   繁体   English

Scala类型边界和方差

[英]Scala type bounds & variance

I am trying to get a better understanding of the following behaviour: 我试图更好地理解以下行为:

scala> class C[-A, +B <: A]
<console>:7: error: contravariant type A occurs in covariant position
                    in type >: Nothing <: A of type B
       class C[-A, +B <: A]
                    ^

However the following works: 但是以下工作:

scala> class C[-A, +B <% A]
defined class C

I can see that there could be issues from the variance of the bounding and bounded variables being opposite, though I have am not clear on what the specific problem is. 我可以看到边界和有界变量的方差可能存在问题,尽管我不清楚具体问题是什么。 I am even less clear on why changing the type bound to a view bound makes things ok. 我更不清楚为什么更改绑定到视图绑定的类型会使事情变得正常。 In the absence of applicable implicit conversions I would expect the two definitions to be have largely the same effect. 在没有适用的隐式转换的情况下,我希望这两个定义具有大致相同的效果。 If anything I would expect a view bound to provide more opportunities for mischief. 如果有什么我会期望一个观点必然会提供更多的恶作剧机会。

For a bit of background I defining classes that are in some ways like functions, and I wanted to do something like 对于一些背景,我定义了类似函数的类,我想做类似的事情

CompositeFunc[-A, +B <: C, -C, +D] (f1 : BaseFunc[A, B], f2 : BaseFunc[C, D]) 
  extends BaseFunc[A, D]

Arguably 按理说

CompositeFunc[-A, +B <% C, -C, +D] (f1 : BaseFunc[A, B], f2 : BaseFunc[C, D]) 
  extends BaseFunc[A, D]

is actually preferable, but I would still like to better understand what is going on here. 实际上是可取的,但我仍然希望更好地了解这里发生了什么。

First the easy one: 首先是简单的一个:

class C[-A, +B <% A]

This is equivalent to 这相当于

class C[-A, +B](implicit view: B => A)

Since view is not publically returned, it is not in a position which would constrain the variance of A or B . 由于view未公开返回,因此它不会限制AB的方差。 Eg 例如

class C[-A, +B](val view: B => A)  // error: B in contravariant position in view

In other words, C[-A, +B <% A] is no different than C[-A, +B] in terms of constraints, the view argument doesn't change anything. 换句话说,就约束而言, C[-A, +B <% A]C[-A, +B]没有区别,视图参数不会改变任何东西。


The upper bound case C[-A, +B <: A] I'm not sure about. 上限情况C[-A, +B <: A]我不确定。 The Scala Language Specification in §4.5 states §4.5中的Scala语言规范说明

The variance position of the lower bound of a type declaration or type parameter is the opposite of the variance position of the type declaration or parameter. 类型声明或类型参数的下限的方差位置与类型声明或参数的方差位置相反。

The variance of B does not seem to be involved, but generally the upper bound must be covariant: 似乎没有涉及B的方差,但通常上限必须是协变的:

trait C[-A, B <: A] // contravariant type A occurs in covariant position

This must somehow produce a problem? 这必须以某种方式产生问题? But I couldn't come up with an example which proofs that this construction becomes unsound in a particular case.... 但我无法想出一个例子,证明这种结构在特定情况下变得不健全......


As for the composed function, why not just 至于组合功能,为什么不呢

class Composite[-A, B, +C](g: A => B, h: B => C) extends (A => C) {
  def apply(a: A) = h(g(a))
}

EDIT : For example: 编辑 :例如:

import collection.LinearSeq

def compose[A](g: Traversable[A] => IndexedSeq[A], h: Traversable[A] => LinearSeq[A]) =
  new Composite(g, h)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM