https://kotlinlang.org logo
#compiler
Title
# compiler
a

Alexander Maryanovsky

05/31/2022, 9:34 AM
Someone is confused here. Is it IntelliJ or the compiler? Kotlin 1.6.21
I guess the compiler isn’t yet sure it’s going to be a list of
String
in the middle of the builder block, because I may add some other type later?
When I do
Copy code
val l = buildList {
    add("Hello")
    val s = get(0)
    s.
}
with the caret after the dot, IntelliJ suggests to insert
length
but then says unresolved reference. It does think the type of
s
is
String
e

ephemient

05/31/2022, 9:44 AM
a

Alexander Maryanovsky

05/31/2022, 9:45 AM
Yeah
But I wonder why the compiler can’t infer the type. Is it because it processes the block statement-by-statement?
e

ephemient

05/31/2022, 9:49 AM
@BuilderInference
limitation. it only analyzes the type of the lambda after it has been processed
a

Alexander Maryanovsky

05/31/2022, 10:04 AM
I see
6 Views