Is this a bug?

I wrote the next code and it runs well saying me that variable a is an Int. But if I explicitly define it as an Int I get a compile error. What is happening?

fun main(args: Array<String>) {
    //writing "val a:Int" doesn't compile
    val a = listOf(1, 2, 3, 4, 5).let letLoop@{
        it.forEach{ number -> 
            if (number == 3) {
                return@letLoop number
            }
        }
    }
    println("${a::class.qualifiedName}")
    println(a is Int)
    println(a)
}

It’s not obvious at first glance, but since the compiler does not “know” that the iterated list is not empty, as far as the compiler is concerned, the let block might return Unit (as well as Int). Thus the static type of variable a should be Any.

Your program does not say that variable a is Int but that, in this execution, its value is an Int.

Thanks!! I started to learn Kotlin this week and I needed some help to understand this but it’s clear now :smiley: .