How does Option act like a collection, when it isn't one?

北慕城南 提交于 2019-12-02 07:00:43

问题


The accepted answer to "How to convert a Some(“ ”) to None in one-line?" took the form:

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

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?


回答1:


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).

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

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.



来源:https://stackoverflow.com/questions/9585629/how-does-option-act-like-a-collection-when-it-isnt-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!