Last active
April 7, 2016 00:07
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Handy to use this
implicit class
for logging onNone
versusSome(_)
. Enjoy world! =D