简体   繁体   English

Scala Co和相反

[英]Scala Co and Contravariance

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. 这个问题的另一个,是的,我已经在stackoverflow中阅读了很多这个问题,但仍然不理解这个概念及其应用。

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: 因此,我是Scala的新手,和很多人一样,我仍然没有得到Contravariance的概念,我正在阅读《 Programming Scala,第二版》,并在第283页上开始对co和convariance进行解释。以下示例:

gives the hierarchy: 给出层次结构:

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!

Thinking in java i know that the last expression won't compile because CSuper is a Supertype of CSub. 在Java中思考,我知道最后一个表达式不会编译,因为CSuper是CSub的超类型。

What i don't understand is what means a type, in this case the Function1[-C,+C], is contravariant in the first parameter? 我不明白什么是类型,在这种情况下,Function1 [-C,+ C]在第一个参数中是变量吗?

The book says that for contravariance is when where X[String] is a supertype of X[Any], for some type X. 该书说,对于协变性,当where X[String] is a supertype of X[Any], for some type X.

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? co /协方差仅适用于参数化类型的子类,我的意思是由于我们使用的是Function1,所以方差仅适用于Function1的子类型,对吗?

And how does it actually works, and when should i use / need it? 它实际上是如何工作的,我什么时候应该使用/需要它?

if T' is a subclass of T, is Container[T'] considered a subclass of Container[T]? 如果T'是T的子类,那么Container [T']是否被视为Container [T]的子类?

[+T] covariant : C[T'] is a subclass of C[T], [+T] 协变 :C [T']是C [T]的子类,
[-T] contravariant: C[T] is a subclass of C[T'] [-T] 相反: C [T]是C [T']的子类

Function1 is defined as trait Function1[-T1, +R] , so parameters are contravariant and result type is covariant. Function1被定义为trait Function1[-T1, +R] ,因此参数是互变量的,结果类型是协变量的。

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. 这意味着,哪些参数是给定函数的参数类型的类型,而哪个结果类型是给定函数的结果类型的类型的函数本身就是给定函数的子类型。

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类型的f分配不同的函数声明时,只会编译有效的子类型。

Namely, only these function declarations are valid subtypes of C => C : 即,只有这些函数声明是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

Everything else is either supertype of C => C and can't be assigned to f or unrelated type. 其他所有内容都是C => C超类型,并且不能分配给f或无关类型。

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

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