Scala 中 A<:B 和 +B 有什么区别?
Scala有什么区别
[A <: B]
Scala和
[+B]
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
Scala有什么区别
[A <: B]
Scala和
[+B]
?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
Q[A <: B]
表示类Q
可以采用任何属于B
子类的类A
>。Q[+B]
表示Q
可以采用任何类,但如果A
是的子类>B
,则Q[A]
被认为是Q[B]
的子类。Q[+A <: B]
表示类Q
只能获取B
的子类并传播子类关系。当您想做一些通用的事情,但需要依赖
B
中的一组特定方法时,第一个很有用。例如,如果您有一个带有toFile
方法的Output
类,则可以在任何可传递到Q
的类中使用该方法。当您想要使集合的行为与原始类相同时,第二个很有用。如果您使用
B
并创建子类A
,那么您可以在需要B
的任何地方传递A
。但是如果你获取B
、Q[B]
的集合,是不是总能传入Q[A ]
代替?一般来说,不会;在某些情况下,这样做是错误的。但您可以说,使用+B
(协方差;Q
covaries--follows with--B
的子类的继承关系)。Q[A <: B]
means that classQ
can take any classA
that is a subclass ofB
.Q[+B]
means thatQ
can take any class, but ifA
is a subclass ofB
, thenQ[A]
is considered to be a subclass ofQ[B]
.Q[+A <: B]
means that classQ
can only take subclasses ofB
as well as propagating the subclass relationship.The first is useful when you want to do something generic, but you need to rely upon a certain set of methods in
B
. For example, if you have anOutput
class with atoFile
method, you could use that method in any class that could be passed intoQ
.The second is useful when you want to make collections that behave the same way as the original classes. If you take
B
and you make a subclassA
, then you can passA
in anywhere whereB
is expected. But if you take a collection ofB
,Q[B]
, is it true that you can always pass inQ[A]
instead? In general, no; there are cases when this would be the wrong thing to do. But you can say that this is the right thing to do by using+B
(covariance;Q
covaries--follows along with--B
's subclasses' inheritance relationship).我想用更多的例子来扩展 Rex Kerr 的出色答案:
假设我们有四个类:
让我们从方差开始:
如您所见,List 不关心它是否包含动物或汽车。 List 的开发者并没有强制规定,例如只有汽车可以进入 List。
另外:
如果函数需要
List[Animal]
参数,您也可以将List[Dog]
作为参数传递给函数。由于 List 的协方差,List[Dog]
被视为List[Animal]
的子类。如果 List 不变的话,它就不起作用。现在进入类型界限:
如您所见,Barn 是一个仅适用于动物的集合。这里不允许汽车进入。
I would like to extend Rex Kerr's excellent answer with some more examples:
Let's say we have four classes:
Let's start with variance:
As you can see List does not care whether it contains Animals or Cars. The developers of List did not enforce that e.g. only Cars can go inside Lists.
Additionally:
If a function expects a
List[Animal]
parameter you can also pass aList[Dog]
as an argument to the function instead.List[Dog]
is considered a subclass ofList[Animal]
due to the covariance of List. It would not work if List was invariant.Now onto type bounds:
As you can see Barn is a collection only intended for Animals. No cars allowed in here.
我在研究这个问题时发现了这篇博文。对 Scala 方差进行更深入的解释,包括其在类别理论中的理论基础
http://blogs.atlassian.com/2013/01/covariance-and-contravariance-in-scala/
I found this blog post while researching this question. Gives an even deeper explanation of Scala variance including its theoretical basis in Category Theory
http://blogs.atlassian.com/2013/01/covariance-and-contravariance-in-scala/
根据我的理解:
第一个是参数类型界限,在我们的例子中,有一个上限和下限类型界限,它是“类型参数 A,它是 B(或 B 本身)的子类型。
第二个是类定义的方差注释,在我们的例子中,B
Scala 的协变子类化: + Java: ? extends T 协变子类化
Scala: - Java: ? super T 逆变子类化
for my Understanding:
The first is a parameter type bound, there a upper and lower typebounds in our case its a "type parameter A that is a subtype of B (or B itself).
The second is a Variance Annotation for a class defintion, in our case a covariance subclassing of B
Scala: + Java: ? extends T Covariant subclassing
Scala: - Java: ? super T Contravariant subclassing