【问题标题】:Scalaz .option - what is this shorthand for?Scalaz .option - 这个简写是什么?
【发布时间】:2014-06-25 14:41:50
【问题描述】:

我看到以下代码:

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

这是否等同于:

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

我之所以这么问,是因为我很难在 Scalaz 上为这个 .option 方法找到 doco。

【问题讨论】:

    标签: scala scalaz scala-option


    【解决方案1】:

    不,这段代码相当于:

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

    if (x == 1) Option(...) 的结果类型是Any(就像if (x == 1) Option(...) else ())。

    Scalaz documentation:

    如果condtrue,则返回Some 中的给定参数,否则返回None

    另见Scalaz cheat sheet

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

    【讨论】:

    • 谢谢 - 除了阅读库源代码之外,ScalaZ .option 函数上的任何文档?
    猜你喜欢
    • 2014-01-21
    • 2012-08-21
    • 2014-12-29
    • 2019-06-07
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多