Quantcast
Viewing latest article 26
Browse Latest Browse All 48

Answer by LMeyer for Implicitly convert an Iterable to Option

Implicit conversion alone is not going to help you if you want to do this. What you need here is an implicit class :

object Rich {   implicit class RichIterable[T](it: Iterable[T]){     def toOption: Option[Iterable[T]] = if(it.isEmpty) None else Some(it)   }}scala> import Rich._  import Rich._scala> List(1,2,3).toOption  res0: Option[Iterable[Int]] = Some(List(1, 2, 3))scala> List().toOption  res1: Option[Iterable[Nothing]] = None

Viewing latest article 26
Browse Latest Browse All 48

Trending Articles