🐦 Kotlin
Advanced
What is the buildList function in Kotlin?
Answer
buildList (along with buildMap and buildSet) creates an immutable collection using a builder DSL block. Inside the block, you use a MutableList to add elements, and the result is a read-only List. Example: val items = buildList { add("first"); addAll(fetchItems()); if (hasExtra) add("extra") }. This is cleaner than the alternative of creating a mutableListOf(), populating it with logic, then calling .toList(). The intent (building an immutable list) is immediately clear from the call site, and you cannot accidentally forget the .toList() conversion.
Previous
What is Flow.stateIn() in Kotlin?
Next
How does Kotlin handle Java interoperability with nullable types?