1

I'm seeing the following code:

val a  = (x: Int) => (x == 1).option(doSomethingUnrealiable(1))

Is this equivalent to:

val a  = (x: Int) => if (x == 1) Option(doSomethingUnrealiable(1))

I ask because I'm struggling to find doco on Scalaz for this .option method.

hawkeye
  • 34,745
  • 30
  • 150
  • 304

1 Answers1

4

No, this code is equivalent to:

<...> if (x == 1) Some(doSomethingUnrealiable(1)) else None

Result type of if (x == 1) Option(...) is Any (just like if (x == 1) Option(...) else ()).

See Scalaz documentation:

Returns the given argument in Some if cond is true, None otherwise.

See also Scalaz cheat sheet:

(1 < 10) option 1 assert_=== 1.some
senia
  • 37,745
  • 4
  • 88
  • 129