# For comprehension with HashMap and Option results in confusing error messages

#1

(topic withdrawn by author, will be automatically deleted in 24 hours unless flagged)

#2

Well, Option[A].flatMap wants an A => Option[B].

The reason List[A].flatMap accepts an A => Option[B] is that an Option can be implicitly converted into an Iterable, but it doesn’t work the other way round. After all, what kind of Option would List(1, 2, 3) be?

So:

for { a <- List(1); b <- List(2) } yield (a + b) // List(3)

for { a <- Some(1); b <- Some(2) } yield (a + b) // Some(3)

for { a <- List(1); b <- Some(2) } yield (a + b) // List(3)

for { a <- Some(1); b <- List(2) } yield (a + b) // error: found List, expected Option

A possible workaround may be be to convert all your Options into Lists.

Help understanding compiler errors regarding Option and Map
#3

Looks like you responded to a message I deleted. Sorry I didn’t delete it fast enough.

#4

When you say convert the option to list, do you mean that I should write a function such as the following?

``````def optionToList[A](o:Option[A]):List[A] = {
o match {
case None => Nil
case Some(a) => List(a)
}
}
``````

Or can I do it with some clever `flatMap` call?

#5

Just Option.toList.