问题
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