Add persistent Collection builder functions#166
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
fe58e7d to
bc52615
Compare
qurbonzoda
left a comment
There was a problem hiding this comment.
Documentation for the functions should be amended. Otherwise, LGTM.
bc52615 to
20ad7a1
Compare
| * The list passed as a receiver to the [builderAction] is valid only inside that function. | ||
| * Using it outside the function produces an unspecified behavior. |
There was a problem hiding this comment.
Builders in kotlinx.collections.immutable are capable of constructing a new persistent collection multiple times. However, I'm struggling to identify a compelling use case for retaining a reference to the builder and utilizing it outside the builderAction. If we constraint the builder to be valid only within the function, could using a MutableList as the receiver be a more performance-efficient choice?
There was a problem hiding this comment.
It feels like if that were the case, mutate would already be optimizing this in the same way, no?
62dfb51 to
c90d590
Compare
c90d590 to
1ac235e
Compare
| @OptIn(ExperimentalTypeInference::class, ExperimentalContracts::class) | ||
| public inline fun <T> buildPersistentList(@BuilderInference builderAction: PersistentList.Builder<T>.() -> Unit): PersistentList<T> { | ||
| contract { callsInPlace(builderAction, InvocationKind.EXACTLY_ONCE) } | ||
| return persistentListOf<T>().builder().apply(builderAction).build() | ||
| } |
There was a problem hiding this comment.
- Annotate
@BuilderInferencefor better generic type support. - Add
InvocationKind.EXACTLY_ONCEcontract to indicate the function parameter will be invoked exactly one time.
There was a problem hiding this comment.
AFAIK @BuilderInference is a no-op, and has been for a while now. My understanding is that builder inference is applied by default everywhere
There was a problem hiding this comment.
I didn't remove it to keep the same behaviors as the builders in stdlib.
There was a problem hiding this comment.
The opt-in for ExperimentalTypeInference::class is unnecessary now
1ac235e to
f4b5f15
Compare
| @OptIn(ExperimentalTypeInference::class, ExperimentalContracts::class) | ||
| public inline fun <T> buildPersistentList(@BuilderInference builderAction: PersistentList.Builder<T>.() -> Unit): PersistentList<T> { | ||
| contract { callsInPlace(builderAction, InvocationKind.EXACTLY_ONCE) } | ||
| return persistentListOf<T>().builder().apply(builderAction).build() |
There was a problem hiding this comment.
Could use mutate instead. Same for the other builder functions
There was a problem hiding this comment.
Good catch! Replaced.
c2ade34 to
505c5c2
Compare
|
It looks like you lost the |
|
Oooo, looks like |
505c5c2 to
d0eac5a
Compare
|
@kotlin.internal.InlineOnly
public inline fun <T> T.apply(block: T.() -> Unit): T {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
block()
return this
} |
|
Yes, but contracts currently aren't inferred nor inherited automatically like this, so sadly you have to explicitly add the contract to |
|
I believe we should add contracts in a separate PR. |
d0eac5a to
16df77e
Compare

Closes #137.