Looking at some scala 2.10.4 library code:
implicit def wrapIntArray(xs: Array[Int]): WrappedArray[Int] = if (xs ne null) new WrappedArray.ofInt(xs) else null
What are the issues in making it:
if (xs != null) new WrappedArray.ofInt(xs) else null
Looking at some scala 2.10.4 library code:
implicit def wrapIntArray(xs: Array[Int]): WrappedArray[Int] = if (xs ne null) new WrappedArray.ofInt(xs) else null
What are the issues in making it:
if (xs != null) new WrappedArray.ofInt(xs) else null
Ideally this should be written to avoid nulls altogether, e.g.
implicit def wrapIntArray(xs: Array[Int]): Option[WrappedArray[Int]] =
Option(xs).map(a => new WrappedArray.ofInt(xs))
However given that this is a library function, changing its signature might not be possible. We could still use the Option approach internally though:
implicit def wrapIntArray(xs: Array[Int]): WrappedArray[Int] =
Option(xs).map(a => new WrappedArray.ofInt(xs)).getOrElse(null)
Consider writing this as:
implicit def wrapIntArray(xs: Array[Int]): Option[WrappedArray[Int]] = Option(xs).map(new WrappedArray.ofInt)
and work within the Option "context" (Option(xs) will be None if xs is null) rather than dealing with null.