Why does `intRange.endInclusive` produce a warning?

Viewed 264

The following code produces the lint warning "Could be replaced with unboxed last":

fun foo() {
    val range = 1..3
    range.endInclusive
}

Screenshot

Replacing endInclusive with last clears the warning.

But why? What is wrong with this code? I would have expected endInclusive to be the correct property to use for an IntRange.

(I'm using Kotlin 1.3.70 in Android Studio 3.6.1.)

1 Answers

Class IntRange inherits class IntProgression and implements interface ClosedRange<Int>.

last is a property of class IntProgression. This class is not generic, property's type is Int and it does not have custom getter/setter. last is translated into a method getLast() that returns a value of unboxed type int.

endInclusive is an abstract property of interface ClosedRange<Int>. This interface is generic, property's type is defined as T and, moreover, its implementation in class IntRange has a custom getter (which just returns last). endInclusive is translated into a method getEndInclusive() that returns a value of boxed type Integer.

Related