- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
option2Iterable ambiguous with itself
Fri, 2009-02-27, 04:08
Welcome to Scala version 2.7.3.final (Java HotSpot(TM) 64-Bit Server
VM, Java 1. 6.0_07).
This is normal enough...
scala> val f = for (x <- (1 to 3).force; y <- Some(1)) yield y
f: Seq[Int] = Array(1, 1, 1)
But try to yield an Option...
scala> val f = for (x <- (1 to 3).force; y <- Some(1)) yield Some(y)
:4: error: type mismatch;
found : Option[Some[Int]]
required: Iterable[?]
Note that implicit conversions are not applicable because they are ambiguous:
both method option2Iterable in object Option of type [A](Option[A])Iterable[A]
and method option2Iterable in object Option of type [A](Option[A])Iterable[A]
are possible conversion functions from Option[Some[Int]] to It...
val f = for (x <- (1 to 3).force; y <- Some(1)) yield Some(y)
Notice the two option2Iterable lines said to be ambiguous are identical.
I have some code that is trying to produce a Seq of tuples, but if one
of the elements of the tuple is an Option, I get this error. Can I
restructure my for expression to make this work?
On Fri, Feb 27, 2009 at 4:07 AM, J Robert Ray wrote:
> Welcome to Scala version 2.7.3.final (Java HotSpot(TM) 64-Bit Server
> VM, Java 1. 6.0_07).
>
> This is normal enough...
>
> scala> val f = for (x <- (1 to 3).force; y <- Some(1)) yield y
> f: Seq[Int] = Array(1, 1, 1)
>
> But try to yield an Option...
>
> scala> val f = for (x <- (1 to 3).force; y <- Some(1)) yield Some(y)
> :4: error: type mismatch;
> found : Option[Some[Int]]
> required: Iterable[?]
> Note that implicit conversions are not applicable because they are ambiguous:
> both method option2Iterable in object Option of type [A](Option[A])Iterable[A]
> and method option2Iterable in object Option of type [A](Option[A])Iterable[A]
> are possible conversion functions from Option[Some[Int]] to It...
> val f = for (x <- (1 to 3).force; y <- Some(1)) yield Some(y)
>
> Notice the two option2Iterable lines said to be ambiguous are identical.
>
Yes, I came across this recently as well. It's a bug in the implicits
resolution, which is by now fixed in trunk.
For earlier versions of the distribution you can always write
val f = for (x <- (1 to 3).force; y <- Some(1).toList) yield Some(y)
Hope this helps