Bug in Array.sort() extension function


On http://kotlin-demo.jetbrains.com/

fun main(args : Array<String>) {
  val nums = array (5,4,3,2,1)
  nums forEach {print (it)}

produces output

Generated classfiles: _DefaultPackage.class _DefaultPackage-dummy-b9f6f59e.class _DefaultPackage$main$1.class 23451

The root cause seems to be that the the default value of toIndex in the following extension function assumes an end-inclusive range (probably because ranges are currently end-inclusive in Kotlin). However, ranges are end-exclusive in Java.

/** * Sorts array or range in array inplace */ public fun <T> Array<out T>.sort(fromIndex : Int = 0, toIndex : Int = size - 1) : Unit {   Arrays.sort(this, fromIndex, toIndex) }

I think one can count this bug as a point in favour of making end-exclusivity the convention in Kotlin as I’ve argued here: http://devnet.jetbrains.com/thread/455071?tstart=0. An end-inclusive Kotlin interoperating with end-exclusive Java seems like it would be prone to errors.


Already fixed in master.

You are right: the fact that Java’s libraries have end-exclusive conventions is a point for having end-exclusivity in Kotlin as well.