Peter
06/22/2023, 9:21 PMbuildList
if I modify a list but at the end of the method want to return a read-only version of that same list. However because buildList uses a mutable-list as its receiver, it often makes invoking other methods in the same object a bit clumsy. Suddenly my code is full of this@… syntax.
So what I do is define something like this and use that instead:
inline fun <E> createList(
capacity: Int = 10,
builderAction: (MutableList<E>) -> Unit
): List<E> = _buildList_(capacity) *{*
builderAction(this)
}
But of course that is not ideal. So I was wondering if there are proposals on the horizon to make buildList (or other similar scenarios) nicer to use (like would multiple-receivers also solve this use-case)?
Or perhaps people don’t mind the this@.. syntax that much?Youssef Shoaib [MOD]
06/22/2023, 10:53 PMthis@
that often thenDerek Peirce
06/23/2023, 2:09 AMinline fun <E> createList(
capacity: Int = 10,
builderAction: MutableList<E>.() -> Unit
): List<E> = buildList(capacity) {
builderAction()
}
which also allows the lambda passed into createList
to behave more like a lambda passed to the original buildList
.Peter
06/23/2023, 7:26 AM