Skip to content

Commit

Permalink
Add Option.{when,unless}
Browse files Browse the repository at this point in the history
  • Loading branch information
japgolly committed Jul 16, 2017
1 parent 12a0313 commit 44d08f6
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/library/scala/Option.scala
Expand Up @@ -28,6 +28,19 @@ object Option {
* the collections hierarchy.
*/
def empty[A] : Option[A] = None

/** When a given condition is true, evaluates the `a` argument and returns
* Some(a). When the condition is false, `a` is not evaluated and None is
* returned.
*/
def when[A](cond: Boolean)(a: => A): Option[A] =
if (cond) Some(a) else None

/** Unless a given condition is true, this will evaluate the `a` argument and
* return Some(a). Otherwise, `a` is not evaluated and None is returned.
*/
@inline def unless[A](cond: Boolean)(a: => A): Option[A] =
when(!cond)(a)
}

/** Represents optional values. Instances of `Option`
Expand Down

0 comments on commit 44d08f6

Please sign in to comment.