0

If I have a List of tuples, I can convert to a map with toMap:

val x = (3 -> 3)
List(x).toMap

and I get

scala.collection.immutable.Map[Int,Int] = Map(3 -> 3)

If I have a List of Optional and try the same I would get an error:

val x = Some(3 -> 3)
val y = None
List(x, y).toMap

<console>:15: error: Cannot prove that Some[(Int, Int)] <:< (T, U).

I want to get the same result. Is it possible?

pedrorijo91
  • 7,635
  • 9
  • 44
  • 82

1 Answers1

5

You can use flatten on the List to remove the Nones:

val x = Some(3 -> 3)
val y = None
List(x, y).flatten.toMap

> scala.collection.immutable.Map[Int,Int] = Map(3 -> 3)
Jean Logeart
  • 52,687
  • 11
  • 83
  • 118