This is for compatibility with Java and for speed. You will need to double the answer (first in java.lang.Integer , then Option ) instead of just returning a negative number. This may take about ten times.
You can always write something that converts negative numbers to None and non-negative to Some(n) if this bothers you:
implicit class OptionOutNegatives(val underlying: Int) extends AnyVal { def asIndex = if (underlying < 0) None else Some(underlying) }
source share