Itโs just for historical reasons, but then I want to know what historical reasons are: what was the story and why did it happen?
The immediate history is the java.lang.String.indexOf method, which returns the index, or -1 if no matching character is found. But this is hardly new; the Fortran SCAN function returns 0 if the character is not found in the string, which is the same as that in which Fortran uses 1-indexing.
The reason for this is that the strings have only a positive length, so any negative length can be used as a None value without any boxing overhead. -1 is the most convenient negative number, so itโs
And this can add up if the compiler is not smart enough to understand that everything is boxing and unboxing and everything does not matter. In particular, creating an object tends to take 5-10 ns, while calling or comparing functions usually takes more than 1-2 ns, so if the collection is short, creating a new object can have a significant fractional penalty (moreover, if your memory is already taxed tax free, and GC has a lot of work).
If Scala initially had an amazing optimizer, then the choice would probably be different, since you could just write things with options, which would be safer and less specific, and then trust the compiler to convert it to the corresponding high-performance code.
source share