Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does `intRange.endInclusive` produce a warning? [duplicate]

Tags:

kotlin

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.)

like image 818
Tom B Avatar asked Oct 24 '25 15:10

Tom B


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.

like image 96
ardenit Avatar answered Oct 27 '25 06:10

ardenit