In Scala, why do I get this “polymorphic expression cannot be instantiated to expected type”?

前端 未结 3 691
星月不相逢
星月不相逢 2020-12-17 08:33

Why does the following occur in Scala 2.9.0.1?

scala> def f(xs: Seq[Either[Int,String]]) = 0
f: (xs: Seq[Either[Int,String]])Int

scala> val xs = List(         


        
3条回答
  •  无人及你
    2020-12-17 09:39

    The best person to explain this is Adriaan Moors, and he already did that here on Stack Overflow -- lookup answers from him and you'll find it.

    Anyway, the problem is that the type of List(Left(0), Right("a")).iterator.toArray cannot be inferred within the boundaries expected by f. It does not conform to Seq[Either[Int, String]] without an implicit conversion, and no implicit conversion can be applied because it (the type) cannot be determined. It's like an egg&chicken problem.

    If you use <% or assign it to a val, you break the cycle in the inference.

提交回复
热议问题