This page is no longer maintained — Please continue to the home page at www.scala-lang.org

option2Iterable ambiguous with itself

1 reply
J Robert Ray
Joined: 2008-12-18,
User offline. Last seen 3 years 16 weeks ago.

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?

odersky
Joined: 2008-07-29,
User offline. Last seen 45 weeks 6 days ago.
Re: option2Iterable ambiguous with itself

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

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland