https://kotlinlang.org logo
#getting-started
Title
# getting-started
s

Shehab Ellithy

10/04/2023, 1:23 AM
Want to understand (more deeply) the purpose of
buildList
and family. To me it's semantics instead of (the written-on-phone hence untested block)
Copy code
mutableListOf<Int>().run {
    add(1)
    this as List<Int>
}
Here're the advantages I see: 1. `buildList`'s usage, semantically, is clearer 2. No need to cast (but can just specify the type somewhere) 3.
buildList
(after the work on the Builder annotation) can infer types so no need to specify any types explicitly Am I missing something else? Pure judgment call here, but isn't the work on
ListBuilder
and builder annotation too much just for this? Only point 3 to me seems that worth it to include directly in the language
e

ephemient

10/04/2023, 1:28 AM
buildList
behaves more like
Copy code
mutableListOf<Int>().run {
    add(1)
}.toList()
because the resulting list is actually immutable, not just by the
List
type, but also by overriding the mutation methods (since the underlying
java.util.List
interface methods may be accessed despite the
kotlin.collections.List
interface not containing them)
but unlike that snippet, it does so without the extra list copy
it also provides a nice, consistent, parallel API to other more complex ones such as the
flow
builder which really doesn't have a good alternative
s

Shehab Ellithy

10/04/2023, 1:39 AM
So efficiency is a goal too. Not sure what
flow
builder is (and Google not helping)
s

Shehab Ellithy

10/04/2023, 1:43 AM
I see. Didn't know they're called
flow
builders
e

ephemient

10/04/2023, 1:44 AM
only the
flow
one is a flow builder, the others are just builders in the same style
1
(that similarly can't be easily represented otherwise)
s

Shehab Ellithy

10/04/2023, 1:44 AM
So, I'm guessing, there was a need to build (relatively large) immutable lists efficiently thats why this came about
Still doesn't feel too good...
why doesn't it?
s

Shehab Ellithy

10/04/2023, 1:54 AM
Ok that YouTrack makes a lot more sense, thanks!
7 Views