Jan. 3rd, 2016
two variances in Scala
Jan. 3rd, 2016 08:12 pmscala> val s = Set("a", "b", "c") s: scala.collection.immutable.Set[String] = Set(a, b, c) scala> val t = s.map(_ + ":)") t: scala.collection.immutable.Set[String] = Set(a:), b:), c:)) scala> val s = Set("a1", "a2", "a3") s: scala.collection.immutable.Set[String] = Set(a1, a2, a3) scala> val t = s.map(_ take 1) t: scala.collection.immutable.Set[String] = Set(a) scala> val u:Set[Any] = s map identity u: Set[Any] = Set(a1, a2, a3) scala> val v:Set[Any] = s <console>:8: error: type mismatch; found : scala.collection.immutable.Set[String] required: Set[Any] Note: String <: Any, but trait Set is invariant in type A. You may wish to investigate a wildcard type such as `_ <: Any`. (SLS 3.2.10) val v:Set[Any] = s ^
From a categorist's p.o.v, wtf, if we have
map
, we have a covariant functor. But wow, it's "type theory", covariance here means only covariance w.r.t. subtyping. So, big deal, map with identity, no? I mean, not being a typist, I don't even understand the problem. Do you?