当 Option 不是一个集合时,它如何表现得像一个集合? [英] How does Option act like a collection, when it isn't one?

查看:62
本文介绍了当 Option 不是一个集合时,它如何表现得像一个集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

How to convert aSome("") to None in one line?" 的形式如下:

def convert(x: Option[String]) : Option[String] = 
    x.map(_.trim()).filterNot(_.isEmpty())

我的问题是我不知道如何找到 filterNot 返回的集合转换为 Option 的方式.我查看了 Scaladoc 中的 Option 构造函数、Option Object、Predef、Seq 和 Seq Object.我想某处可能有一个隐含的东西,但是人们如何找到它呢?

My problem is that I can't figure out how to find by what means the collection returned by filterNot is converted to an Option. I looked at the Scaladoc for Option constructors, Option Object, Predef, Seq, and Seq Object. I figure there's probably an implicit somewhere, but how does one go about finding it?

推荐答案

在 scaladoc 中,您可以单击按继承"排序按钮(它出现在方法描述的正上方).这应该可以帮助您找到给定方法的具体实现.跳转到那个类并查看源代码(指向源代码的链接出现在类/特征/对象描述中).

In scaladoc, you can click the "by inheritance" ordering button (it appears just above the methods description). This should help you finding the concrete implementation of a given method. The jump to that class and have a look to the source (the links to the source appears in the class/trait/object description).

在Options的情况下,有mapfilter等方法

In the case of Options, there are methods called map, filter, etc.

当需要隐式值时,您会注意到,要么是参数列表中的 implicit 子句,要么是参数中像 [A: MyImplicit] 这样的上下文绑定.隐式通常在伴随对象中导入或声明.

When an implicit value is required, you will notice, either an implicit clause in the argument list, or a context bound like [A: MyImplicit] in the parameters. Implicit are usually imported or declared in the companion object.

如果您有更多规范问题/示例,请告诉我.

If you have a more specification question/example, please let me know.

这篇关于当 Option 不是一个集合时,它如何表现得像一个集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆