Skip to content

Instantly share code, notes, and snippets.

@malcolmgreaves
Last active April 7, 2016 00:07
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 malcolmgreaves/91cc4fb590d18dfa72ac8e3142cd3af6 to your computer and use it in GitHub Desktop.
Save malcolmgreaves/91cc4fb590d18dfa72ac8e3142cd3af6 to your computer and use it in GitHub Desktop.
Perform a side-effecting function on an Option's value and allow for chaining.
implicit class AddSideEffectOpToOption[T](private val x: Option[T]) extends AnyVal {
@inline def sideEffectOnly(ifNone: => Unit, ifSome: T => Unit): Option[T] = {
x match {
case None => ifNone
case Some(value) => ifSome(value)
}
x
}
@inline def sideEffectOnly(ifNone: => Unit): Option[T] = {
x match {
case None => ifNone
case _ => ()
}
x
}
@inline def sideEffectOnly(ifSome: T => Unit): Option[T] = {
x match {
case Some(value) => ifSome(value)
case _ => ()
}
x
}
}
@malcolmgreaves
Copy link
Author

Handy to use this implicit class for logging on None versus Some(_). Enjoy world! =D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment