开发者

Variance of Ordered, PartiallyOrdered

According to the documentation, PartiallyOrdered[A] is covariant in A, while Ordered[A] is invariant (but used to be covariant) in A.

Why was Ordered[A] ever covariant in A? Isn't this an obvious violation of the substitution principle?

Why can't Ordered[A] be contravariant in A? This would allow an Ordered[Traversible[Char]] to be typed as an Ordered[StringBuilder], for example. I don't see how this could be problematic.

I'm having trouble understanding the signature of tryCompareTo in PartiallyOrdered. It looks like the argument can be an instance of any supertype of A. Couldn't you pass in any object by calling tryCompareTo[Any](anything)? If so, how is the method signature any better than tr开发者_StackOverflow中文版yCompareTo(that: Any)?

Logically, ordered sets are a subclass of partially ordered sets, but the Scala classes don't seem to reflect this relationship. Is this because Ordered[A] cannot be covariant in A as PartiallyOrdered[A] can?


Looking at the previous version (2.8.1) , I didn't find anything relevant about Ordered that was covariant ?

EDIT : Looked further and find some explanation in the documentation for Ordered to not be covariant anymore.

For the case of tryCompare in PartiallyOrdered, the signature set more restriction on passed parameter :

def tryCompareTo [B >: A] (that: B)(implicit arg0: (B) ⇒ PartiallyOrdered[B]): Option[Int]

Here the implicit arg0 implies that to be seen as a PartiallyOrdered[B] instance and thus that it has access to all method defined in this trait.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜