soc*_*soc 11 methods null scala standard-library scala-option
我想知道标准库是完全null免费的 - 如果不是 - 会感兴趣的是合理的用例存在哪些返回null比返回一些Option实例更可取.
我null在标准库中看到的唯一一个地方是可选的正则表达式组.
scala> val number = """(\d+)(\.\d+)?""".r // matches digits optionally followed by a . and more digits
number: scala.util.matching.Regex = (\d+)(\.\d+)?
scala> "12" match {
| case number(intPart, decimalPart) => (intPart, decimalPart)
| }
res0: (String, String) = (12,null)
Run Code Online (Sandbox Code Playgroud)
我认为,这里的推理是你不想Option[String]用于所有群体.如果组不是可选的,这将使代码不必要地笨拙.不幸的是,如果组是可选的,则在编译时不知道.所以它Option[String]适用于所有组或null不匹配组.
NamespaceBinding返回null本地命名空间,或者在以下情况下返回未定义的输入.
$ scala
Welcome to Scala version 2.9.0.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_24).
Type in expressions to have them evaluated.
Type :help for more information.
scala> (<foo/>).scope.getURI("something")
res0: String = null
Run Code Online (Sandbox Code Playgroud)
我不知道为什么用它String代替Option[URI].
| 归档时间: |
|
| 查看次数: |
590 次 |
| 最近记录: |