简体   繁体   English

Scalaz .option-这是什么速记?

[英]Scalaz .option - what is this shorthand for?

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. 我问是因为我很难在Scalaz上为该.option方法找到doco。

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 () ). if (x == 1) Option(...)结果类型是Any (就像if (x == 1) Option(...) else () )。

See Scalaz documentation : 请参阅Scalaz文档

Returns the given argument in Some if cond is true , None otherwise. 如果condtrue ,则返回Some中的给定参数,否则返回None

See also Scalaz cheat sheet : 另请参阅Scalaz备忘单

(1 < 10) option 1 assert_=== 1.some

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM