Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lists and Sets should provide an implementation for plusElement #141

Open
SebastianAigner opened this issue Jan 19, 2023 · 1 comment
Open

Comments

@SebastianAigner
Copy link
Member

SebastianAigner commented Jan 19, 2023

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:

var a = persistentListOf<Int>()
a += 1

but this code doesn't:

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:
image

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.

@SebastianAigner
Copy link
Member Author

SebastianAigner commented Jan 19, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant