I have the following simple wrapper class and an implicit converter to automatically wrap values:
import scala.language.implicitConversions
case class Wrap[T](t: T)
implicit def autoWrap[T](t: T): Wrap[T] = Wrap(t)
This works fine mostly:
scala> val w: Wrap[_] = 5
w: Wrap[_] = Wrap(5)
scala> val w: Wrap[_] = Set(1,2,3)
w: Wrap[_] = Wrap(Set(1, 2, 3))
Except for in this one instance:
scala> val w: Wrap[_] = Seq(1,2,3).toSet
<console>:15: error: polymorphic expression cannot be instantiated to expected type;
found : [B >: Int]scala.collection.immutable.Set[B]
required: Wrap[_]
val w: Wrap[_] = Seq(1,2,3).toSet
^
If I assign the value first, it also works:
scala> Seq(1,2,3).toSet
res1: scala.collection.immutable.Set[Int] = Set(1, 2, 3)
scala> val w: Wrap[_] = res1
w: Wrap[_] = Wrap(Set(1, 2, 3))
What's going on here?