Scala Option Equivalent to callIfNotEmpty

option-type, scala

Solution

Try using `foreach`. For example:

option.foreach(println)

From the docs:

`final def foreach[U](f: (A) ⇒ U): Unit`

Apply the given procedure `f` to the option's value, if it is nonempty.

There's even a comment at the top of the `Option` code:

The most idiomatic way to use an `scala.Option` instance is to treat it as a collection or monad and use `map`, `flatMap`, `filter`, or `foreach`

Recall that an `Option` can be implicitly converted to an `Iterable`, so anything you usually to on collections is available to you!

Problem

Is there any Option function equivalent to: ``` def callIfNotEmpty[T](option: Option[T], fun: (T) => Unit): Unit = option match { case Some(x) => fun(x) case None => } ``` That could be called as: ``` option.callIfNotEmpty((optionValue) => fun(optionValue)) ```

Original source