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

mutable type in collection #89

Merged
merged 2 commits into from
Jul 5, 2023

Conversation

xiaozhikang0916
Copy link
Contributor

Fix #81

Now items of list/set can be modified like issue #81 shows.

Map and generic type of non-primitive are currently not supported.

@serras
Copy link
Collaborator

serras commented Jul 4, 2023

@xiaozhikang0916 thanks for your contribution! This looks fantastic, let's check that CI is green and then I'll merge

@xiaozhikang0916
Copy link
Contributor Author

The lint issue is fixed.

And, there will be some break change in this pr.

@Test
fun `edit list item in old school style`() {
  """
    data class Group(val p: List<Person>)
    data class Person(val age: Int)
    
    val g1 = Group(listOf(Person(1), Person(2)))
    val g2 = g1.copy { p[1] = p[1].copy(age = 3) }
    val age = g2.p[1].age
  """.trimIndent().evals("age" to 3)
}

This test will fail in this pr but pass in the main branch.

@serras
Copy link
Collaborator

serras commented Jul 5, 2023

This test will fail in this pr but pass in the main branch.

why is it so? Is it because the copy method no longer exists or some other reason?

@xiaozhikang0916
Copy link
Contributor Author

This test will fail in this pr but pass in the main branch.

why is it so? Is it because the copy method no longer exists or some other reason?

    data class Group(val p: List<Person>)
    data class Person(val age: Int)

In main branch, you will get a mutable class of Group like

public class `Group$Mutable`(
  public var p: MutableList<Person>,
  public val old: Group,
)

Inside the copy scope, p[1] returns a instance of Person class, which is a kotlin data class so you can call the copy function.


In this pr, the mutable class of Group becomes

public class `Group$Mutable`(
  public var p: MutableList<`Person$Mutable`>,
  public val old: Group,
)

In the copy scope, p[1] will return the generated mutable class of Person rather than the original data class, which means, no copy function avaliable.


This is an intended change, but also break change. All data access operation of list/set inside a copy scope should have a check, and call of copy function should migrate to value-assign style.

@serras serras merged commit 0d53fd6 into kopykat-kt:main Jul 5, 2023
6 checks passed
@xiaozhikang0916 xiaozhikang0916 deleted the feature/collection branch July 5, 2023 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Mutable type in Collections
2 participants