Вопрос

I just stumbled over the following definition of method to defined by TraversableLike (2.10.0):

override def to[Col[_]](implicit cbf: CanBuildFrom[Nothing, A, Col[A @uV]]): Col[A @uV] = {
  val b = cbf()
  b.sizeHint(this)
  b ++= thisCollection
  b.result
}

According to this answer, the @uV disabled variance checking. That sounds dangerous. Why would I want to do that?

Это было полезно?

Решение

I guess the answer would be very similar to the one for this question: When is @uncheckedVariance needed in Scala, and why is it used in GenericTraversableTemplate?

Since builders (the implicit parameter cbf) exist for immutable (possibly covariant) and mutable (invariant) collections, the annotation is probably used here to make the to method work for both types of collections.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top