Scala Co和相反 [英] Scala Co and Contravariance

查看:113
本文介绍了Scala Co和相反的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的!这个问题的另一个,是的,我已经在stackoverflow中阅读了很多这个问题,但仍然不理解这个概念及其应用。

Yes! Another of this question, and yes i already read alot of this questions in stackoverflow and still don't understand this concept and it's application.

所以,我是我新人来到Scala,和许多人一样,我仍然没有得到Contravariance的概念,我正在阅读《 Programming Scala,第二版》,在第283页上,用以下示例开始对co和convariance的解释:

So, i'm i new comer to Scala, and like many people i still didn't get the concept of Contravariance, i'm reading the Programming Scala, 2nd Edition, and on page 283 starts the explanation of co and contravariance with the following example:

给出了层次结构:

class CSuper { def msuper() = println("CSuper") }
class C extends CSuper { def m() = println("C") }
class CSub extends C { def msub() = println("CSub") }

然后有一个函数和一些使用示例:

then there is a function and some examples of use:

var f: C => C = (c: C) => new C
    f         = (c: CSuper) => new CSub
    f         = (c: CSuper) => new C
    f         = (c: C) => new CSub
    f         = (c: CSub) => new CSuper // COMPILATION ERROR!

在Java中,我知道最后一个表达式不会编译,因为CSuper是CSub的超类型。

Thinking in java i know that the last expression won't compile because CSuper is a Supertype of CSub.

我不明白什么是类型,在这种情况下,Function1 [-C,+ C]在第一个参数中是变量?

What i don't understand is what means a type, in this case the Function1[-C,+C], is contravariant in the first parameter?

这本书说,对于协变性,当时,对于某些类型X,X [String]是X [Any]的超类型。

The book says that for contravariance is when where X[String] is a supertype of X[Any], for some type X.

co /协方差仅适用于参数化类型的子类,我的意思是因为我们使用的是Function1,所以方差仅适用于Function1的子类型,会是这样吗?

The co / contravariance is just appliable in the subclasses of the parameterized types, i mean since we are using a Function1, the variance just applies to subtypes of Function1, would be it?

它是如何工作的,我应该何时使用/需要它?

And how does it actually works, and when should i use / need it?

推荐答案


如果T'是T的子类,那么Container [T']是否被视为Container [T]的子类?

if T’ is a subclass of T, is Container[T’] considered a subclass of Container[T]?

[+ T] 协变:C [T']是C [T]的子类,

[-T] 相反: C [T]是C [T']

[+T] covariant: C[T’] is a subclass of C[T],
[-T] contravariant: C[T] is a subclass of C[T’]

Function1 被定义为 trait Function1 [-T1,+ R] ,因此参数是变量,结果类型是变量。

Function1 is defined as trait Function1[-T1, +R], so parameters are contravariant and result type is covariant.

这意味着,哪些函数是给定函数的参数类型的超类型,哪些结果类型是给定函数的结果类型的 subtype 本身是子类型给定功能的

That means, that functions, which arguments are supertypes of argument type of given function, and which result type is subtype of result type of given function is itself subtype of given function.

在您的示例中,您正在为类型 C的 f 分配不同的函数声明=> C ,只会分配有效的子类型。

In your example, as you are assigning different function declarations to f of type C => C, only assigning of valid subtypes will compile.

即,只有这些函数声明是的有效子类型C = > C

Namely, only these function declarations are valid subtypes of C => C:

var f: C => C = (c: C) => new C
f         = (c: C) => new C
f         = (c: C) => new CSub
f         = (c: CSuper) => new C
f         = (c: CSuper) => new CSub

其他一切都是 C =>的超类型; C ,并且不能分配给 f 或无关类型。

Everything else is either supertype of C => C and can't be assigned to f or unrelated type.

这篇关于Scala Co和相反的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆