While working on this question, I came up with the following issue. Consider two method definitions:
def foo[T <: Ordered[T]](s : Seq[T]) = s.sorted
def foo[T <% Ordered[T]](s : Seq[T]) = s.sorted
The first one compiles, the second does not. The compiler does not figure out that it can use the asserted implicit conversion to get an Ordering
. If we help a bit, it works:
def foo[T <% Ordered[T]](s : Seq[T]) = s.sortWith(_<=_)
While compiling the anonymous function the compiler applies the implicit conversion to find method <=
, everything is fine.
I do not have another example, but can imagine similar issues to happen with other functions on collections that require elements to have certain properties, if those can only be asserted via conversion.
Is there a particular reason why the compiler is restricted this way? Is there no general way to resolve such issues? (Here it seems easy.) Is there a workaround, e.g. another implicit conversion that translates the property on Key[T]
to T
?
(Note that the last idea can be problematic if a concrete value for T
ends up having the property; we then get an ambiguous situation).
By the way, re "here it seems easy", it wasn't. Implicits like these enjoy diverging and they were pretty determined.