Shouldn't `val nodeId: UUID = UUID.randomUUID()` i...
# getting-started
k
Shouldn't
val nodeId: UUID = UUID.randomUUID()
inside a data class generate a new uuid each time I make a new instance of the class?
d
Yes.
💯 3
👍 1
k
Looks like the list.chunked I do after creating the instances confused me.
I thought I would get a different result.
s
You are creating 10 Persons. These 10 Persons have a unique ID each. Then you are assigning the existing 10 Persons to a Group. These do not copy the Persons. The Group holds a reference to the Person. Furthermore, kotlin does not have something like copy constructors which I believe you confuses you. Objects are always passed by reference. You can use the copy() function of data classes to copy the object. That will create a new object with the same values of the existing object. Edit: nvm, misunderstood the issue.
s
Cant you just use .map between .chunked(2) and {Group(it)}
Like so
Copy code
val groups = people.chunked(2).map { Group(it) }
i
@Kenneth Note this paragraph in
chunked
function docs (https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/chunked.html):
... the list passed to the transform function is ephemeral and is valid only inside that function. You should not store it or allow it to escape in some way, unless you made a snapshot of it.
So use the version with
map
proposed by @StevieB or make a snapshot of each group list with the extension `toList()`:
chunked(2) { Group(it.toList()) }