Skip to content

Instantly share code, notes, and snippets.

@duncanmak
Created October 10, 2014 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duncanmak/f19b414f4dc4d923ea83 to your computer and use it in GitHub Desktop.
Save duncanmak/f19b414f4dc4d923ea83 to your computer and use it in GitHub Desktop.
scala> class Foo
defined class Foo
scala> val l = List(1,2,3)
l: List[Int] = List(1, 2, 3)
scala> def f(i: Int): Option[Int] = if (i % 2 == 0) Some(i) else None
f: (i: Int)Option[Int]
scala> l.map(f)
res0: List[Option[Int]] = List(None, Some(2), None)
scala> l.flatMap(f)
res1: List[Int] = List(2)
scala> def f[T <: Foo](i: T): Option[T] = ???
f: [T <: Foo](i: T)Option[T]
scala> l.flatMap(f)
<console>:11: error: polymorphic expression cannot be instantiated to expected type;
found : [T <: Foo]T => Option[T]
required: Int => scala.collection.GenTraversableOnce[?]
l.flatMap(f)
^
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment