Type error

19 views
Skip to first unread message

Meeraj Kunnumpurath

unread,
Jul 21, 2016, 12:18:41 PM7/21/16
to scala-user
Hello,

Could someone please explain to me why I get the error on the last instruction below, where the third one runs without a problem?

1 scala> val l = List(1, 2, 3)

l: List[Int] = List(1, 2, 3)


2 scala> l map {x => Some(x) }

res14: List[Some[Int]] = List(Some(1), Some(2), Some(3))


3 scala> l flatMap {x => Some(x) }

res15: List[Int] = List(1, 2, 3)


4 scala> val f = (x: Int) => Some(x)

f: Int => Some[Int] = <function1>


5 scala> l map f

res16: List[Some[Int]] = List(Some(1), Some(2), Some(3))


6 scala> l flatMap f

<console>:13: error: type mismatch;

 found   : Int => Some[Int]

 required: Int => scala.collection.GenTraversableOnce[?]

       l flatMap f

Thank you
--
Meeraj Kunnumpurath
Director and Executive Principal
Service Symphony Ltd
00 44 7702 693597
00 971 50 409 0169
mee...@servicesymphony.com

Clint Gilbert

unread,
Jul 21, 2016, 12:58:49 PM7/21/16
to scala...@googlegroups.com
An implicit conversion is needed to turn Options into TraversableOnces,
allowing you to mix List and Option monads with flatMap and get a List
back. Someone more knowledgeable should comment, but until then, my
guess is that such a conversion can't be found or applied in the case
where the function f is passed in, for some reason.

On 07/21/2016 12:18 PM, Meeraj Kunnumpurath wrote:
> Hello,
>
> Could someone please explain to me why I get the error on the last
> instruction below, where the third one runs without a problem?
>
> 1 scala> val l = List(1, 2, 3)
>
> l: List[Int] = List(1, 2, 3)
>
>
> 2 scala> l map {x => Some(x) }
>
> res14: List[Some[Int]] = List(Some(1), Some(2), Some(3))
>
>
> 3 scala> l flatMap {x => Some(x) }
>
> res15: List[Int] = List(1, 2, 3)
>
>
> 4 scala> val f = (x: Int) => Some(x)
>
> f: Int => Some[Int] = <function1>
>
>
> 5 scala> l map f
>
> res16: List[Some[Int]] = List(Some(1), Some(2), Some(3))
>
>
> 6 scala> l flatMap f
>
> <console>:13: error: type mismatch;
>
> found : Int => Some[Int]
>
> required: Int => scala.collection.GenTraversableOnce[?]
>
> l flatMap f
>
> Thank you
> --
> /*Meeraj Kunnumpurath*/
> *Director and Executive Principal
> Service Symphony Ltd
> 00 44 7702 693597*
> *00 971 50 409 0169
> mee...@servicesymphony.com <mailto:mee...@servicesymphony.com>*
>
> --
> You received this message because you are subscribed to the Google
> Groups "scala-user" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to scala-user+...@googlegroups.com
> <mailto:scala-user+...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.

--



signature.asc

satorg

unread,
Jul 21, 2016, 4:27:34 PM7/21/16
to scala-user
Clint Gilbert wrote:
An implicit conversion is needed to turn Options into TraversableOnces,
allowing you to mix List and Option monads with flatMap and get a List
back.  Someone more knowledgeable should comment, but until then, my
guess is that such a conversion can't be found or applied in the case
where the function f is passed in, for some reason.

Because the implicit conversion is applied inside the lambda
{x => Some(x) }
to the Some(x) expresson
So this lambda is actually of type
Int => GenTraversableOne[Int]
not just
Int => Some[Int]

 
Reply all
Reply to author
Forward
0 new messages