将 AnyRef 装箱到选项中的方法在哪里?
另外,Scala 有一个“选项”方法,可以根据值是否为 null 将值提升为 Some(value) 或 None ...
我似乎在任何地方都找不到这个 option
方法斯卡拉多克。
Iulian Dragos 的 gdata 客户端项目包含一种可能正是 James 所指的方法。
def option[A <: AnyRef](a: A): Option[A] =
if (a eq null) None else Some(a)
请指出我在 scaladoc 中哪里可以找到这个方法。
PS 我有一个如下所示的方法:
def permutations(s: String): List[String] = ...
对于是否应该将其更改为:我有两种想法:
def permutations(s: Option[String]): List[String] = ...
因为客户端可以使用 null
调用它。目前在第一个实例中,我期望一个 String 参数 &我使用前面提到的 option
方法手动将其装箱。
In this blogpost by James Iry, he writes:
Plus, Scala has an "option" method that promotes a value to either Some(value) or None depending on whether it's null or not ...
I can't seem to find this option
method anywhere in the scaladoc.
Iulian Dragos's gdata client project contains a method that is probably what James was referring to.
def option[A <: AnyRef](a: A): Option[A] =
if (a eq null) None else Some(a)
Please point out where can I find this method in the scaladoc.
P.S. I have a method that looks like this:
def permutations(s: String): List[String] = ...
I'm in 2 minds as to whether I should change it to:
def permutations(s: Option[String]): List[String] = ...
since the client can invoke it with null
. Currently in the first instance, I expect a String parameter & I box it manually using the option
method mentioned previously.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以简单地在其伴随对象上使用 apply 工厂方法:
...或更短的版本:
You can simply use the apply factory method on its companion object:
... or the shorter version:
Scaladoc 页面是 http://www.scala-lang.org /api/current/scala/Option$.html。
要从主选项页面导航到它,请单击页面顶部类名称旁边的圆圈中的大“C”。这会在班级和同伴之间切换。您还可以通过单击班级名称左侧左侧导航栏中的“O”来导航到同伴。
The Scaladoc page is http://www.scala-lang.org/api/current/scala/Option$.html.
To navigate to it from the main Option page click on the big "C" in the circle next to the class name at the top of the page. That toggles between the class and the companion. You can also navigate to the companion by clicking the "O" in the left nav to the left of the class name.