KT-15363 Collection Builders
Added container builders for lists, sets, and maps. The new experimental type inference only works for the simple builders with a single generic type. The versions with nullability and the maps still require explicit specification of the types. Obviously explicit specification is required for all users who are not using the new experimental inference. Improving the type inference is something that should be done separately and many things – including these builders – will benefit from it, however, this is not a blocker for these builders in my opinion.
This commit is contained in:
committed by
Ilya Gorbunov
parent
fa811c731f
commit
bf9d3d87a6
@@ -0,0 +1,51 @@
|
||||
package samples.collections
|
||||
|
||||
import samples.*
|
||||
|
||||
@RunWith(Enclosed::class)
|
||||
class Builders {
|
||||
class Lists {
|
||||
@Sample
|
||||
fun buildListSample() {
|
||||
val x = listOf('b', 'c')
|
||||
|
||||
val y = buildList(x.size + 2) {
|
||||
add('a')
|
||||
addAll(x)
|
||||
add('d')
|
||||
}
|
||||
|
||||
assertPrints(y, "[a, b, c, d]")
|
||||
}
|
||||
}
|
||||
|
||||
class Sets {
|
||||
@Sample
|
||||
fun buildSetSample() {
|
||||
val x = setOf('b', 'c')
|
||||
|
||||
val y = buildSet(x.size + 2) {
|
||||
add('a')
|
||||
addAll(x)
|
||||
add('d')
|
||||
}
|
||||
|
||||
assertPrints(y, "[a, b, c, d]")
|
||||
}
|
||||
}
|
||||
|
||||
class Maps {
|
||||
@Sample
|
||||
fun buildMapSample() {
|
||||
val x = mapOf('b' to 2, 'c' to 3)
|
||||
|
||||
val y = buildMap<Char, Int>(x.size + 2) {
|
||||
put('a', 1)
|
||||
putAll(x)
|
||||
put('d', 4)
|
||||
}
|
||||
|
||||
assertPrints(y, "{a=1, b=2, c=3, d=4}")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user