11

I wonder if the standard library is completely null-free and - if not - would be interested what reasonable use-cases exist where returning null is preferable to returning some Option instance.

giampaolo
  • 6,906
  • 5
  • 45
  • 73
soc
  • 27,983
  • 20
  • 111
  • 215
  • The only reasonable use case for returning `null` is when you are using a library that expects `null` to be returned from a callback as an indication of "no result." This is often a necessary evil when dealing with legacy Java libraries from Scala code. – Tom Crockett May 30 '11 at 21:58
  • In general, the Scala library methods will either return Option or will throw an exception if called inappropriately (for example, head and last on list). I don't know of any that return null. – Kris Nuttycombe May 30 '11 at 22:01

3 Answers3

5

The only place I've seen null used in the standard library are optional regex groups.

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)

I think, the reasoning here is that you don't want to use Option[String] for all groups. This would make the code unnecessarily clumsy, if a group is not optional. Unfortunately, it is not known at compile time if a group is optional. So it's either Option[String] for all groups or null for not matching groups.

kassens
  • 4,405
  • 1
  • 26
  • 26
  • 2
    I really like the fact that regex automatically becomes pattern matcher, and the fact that for 90% of the cases you'd want a straight `String`; but since Scala is almost entirely free of weird exceptions, it may have been a good idea if they used `Option[String]` for consistency. `Option[String]` and/or `List` comes up frequently when parsing Json and I don't think I minded them much since I'd use them within for-comprehension. – Eugene Yokota May 30 '11 at 22:56
  • 2
    @Eugene Tip: if you specify the type, then it won't match against `null`. For example, `case number(intPart: String, decimalPart: String)` won't match the string above. – Daniel C. Sobral May 31 '11 at 20:19
5

NamespaceBinding returns null for the local namespace or in the following case an undefined input.

$ 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

Why it's using String instead of Option[URI], I don't know.

Eugene Yokota
  • 94,654
  • 45
  • 215
  • 319
  • 1
    Related to `scala.xml.Elem` which takes a `String` prefix value that is expected to be `null` for no namespace prefix. Again, why it's not `Option[String]` is beyond me. – Kristian Domagala May 30 '11 at 22:50
-1

I can't think of any so I did a google search in the api (inurl:scala-lang.org/api return +null) and it didn't seem to yield any documented use of null.

May be there exist som internal use. The only reasons I can think of for doing that would be to avoid the extra Some-object or to easy integration with java. Both seem unlikely.

thoredge
  • 12,237
  • 1
  • 40
  • 55