You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
var b = persistentListOf<PersistentList<Int>>()
b += persistentListOf(1)
From the linked issue, via Roman Elizarov:
It looks that the root of the problem is that + is overloaded to mean both plusElement and plus (set union/list concatenation). Until it is addressed, everything else is just workaround.
The workaround in the standard library collections is using the plusElement function. However, no implementation for this function exists for persistent lists, so the implementation returning List<T> is chosen:
This means the APIs between regular read-only lists and persistent lists are quite divergent when it comes to adding elements to collections of collections:
val a = measureTimedValue {
var list =listOf(0)
var llist = listOf<List<Int>>()
repeat(25000) {
list = list.plusElement(it)
llist = llist.plusElement(listOf(1,2,3))
}
llist
}
val b = measureTimedValue {
var list= persistentListOf(0)
var llist = persistentListOf<PersistentList<Int>>()
repeat(25000) {
list = list.plus(it)
llist = llist.add(list)
}
llist
}
It seems to me that PersistentList and PersistentSet should provide an implementation for plusElement to help work around this long-standing resolution issue in the same fashion that the standard library collections do.
The text was updated successfully, but these errors were encountered:
Just a personal opinion point on this as well: Intuitively, adding elements to a PersistentList feels much more like a plus operation than an add operation (add is a term that is used often times with mutable collections – and potentially with a return type of Boolean, whereas plus provides a hint that the result of the expression would likely be a 'new' collection reference).
It seems like plus (and the unfortunate plusElement) would actually already suffice, without an add function at all.
Adding elements to lists of lists unfortunately comes with long-standing resolution issues for the operator functions:
https://youtrack.jetbrains.com/issue/KT-9992
That means the following code works:
but this code doesn't:
From the linked issue, via Roman Elizarov:
The workaround in the standard library collections is using the
plusElement
function. However, no implementation for this function exists for persistent lists, so the implementation returningList<T>
is chosen:This means the APIs between regular read-only lists and persistent lists are quite divergent when it comes to adding elements to collections of collections:
It seems to me that
PersistentList
andPersistentSet
should provide an implementation forplusElement
to help work around this long-standing resolution issue in the same fashion that the standard library collections do.The text was updated successfully, but these errors were encountered: