julien lengrand-lambert
07/27/2022, 1:35 PMcopy
creates shallow, not deep copies. The math checks out for complex types such as lists or other data classes, but not for 'primitives' likes Strings and Enums. Can someone help me clarify this? [Here is a complete example](https://pl.kotl.in/CzsYyKC_l) , but in essence for the given code, I would potentially expect name
to also be mutated in the copy if data classes were purely shallow.
Am I getting this wrong?
import java.util.*
enum class WEAPONS{
AXE, SWORD, WAND, BOW
}
enum class CLASS{
WIZARD, WARRIOR, PALADIN, THIEF
}
data class Origin(val city: String, var country: String)
data class FantasyHero(var name: String, val weapons: MutableList<WEAPONS>, var heroClass: CLASS?, val origin: Origin = Origin("Utrecht", "The Netherlands"))
fun main(){
val gandalf = FantasyHero("Gandalf the Grey", mutableListOf(WEAPONS.WAND), CLASS.WIZARD)
val anotherGandalf = FantasyHero("Gandalf the Grey", mutableListOf(WEAPONS.WAND), CLASS.WIZARD)
val gandalfCopy = gandalf.copy()
val betterGandalf = gandalf.copy(name="Gandalf the White")
println("--------")
println("Changing the first instance's name")
gandalf.name = "Gandalf the White"
println(gandalf.name)
println(anotherGandalf.name)
println(gandalfCopy.name)
println(betterGandalf.name)
println("--------")
println("Changing the first instance's class")
gandalf.heroClass = CLASS.PALADIN
println(gandalf.heroClass)
println(anotherGandalf.heroClass)
println(gandalfCopy.heroClass)
println(betterGandalf.heroClass)
}
--------
Changing the first instance's name
Gandalf the White
Gandalf the Grey
Gandalf the Grey
Gandalf the White
--------
Changing the first instance's class
PALADIN
WIZARD
WIZARD
WIZARD
Sam
07/27/2022, 1:39 PMcopy
creates shallow copies is correctLarry Meadors
07/27/2022, 1:41 PMjulien lengrand-lambert
07/27/2022, 1:42 PMdata class Origin(var city: String, var country: String)
fun main(){
val myHome = Origin("Utrecht", "the Netherlands")
val myHomeCopy = myHome.copy()
myHome.city = "Amsterdam"
println(myHome.city)
println(myHomeCopy.city)
}
Larry Meadors
07/27/2022, 1:43 PMUtrecht
Amsterdam
julien lengrand-lambert
07/27/2022, 1:43 PMSam
07/27/2022, 1:44 PMjulien lengrand-lambert
07/27/2022, 1:44 PMSam
07/27/2022, 1:44 PMLarry Meadors
07/27/2022, 1:44 PMjulien lengrand-lambert
07/27/2022, 1:45 PMSam
07/27/2022, 1:46 PMLarry Meadors
07/27/2022, 1:47 PMSam
07/27/2022, 1:52 PMvar a = "1"
val b = a
a = "2"
println(b) // "1"
same concept, without the data class copy
being involveda
can change which object b
points tojulien lengrand-lambert
07/27/2022, 1:53 PMSam
07/27/2022, 1:56 PMjulien lengrand-lambert
07/27/2022, 1:56 PMLarry Meadors
07/27/2022, 2:13 PMcedric
07/27/2022, 10:34 PMvar
and mutable list that don't seem necessary.julien lengrand-lambert
07/28/2022, 2:52 PMcedric
07/30/2022, 4:35 AMval
and immutable structures are a great default starting point, writing a lot of Rust these past years has made me more comfortable embracing the mutable nature of things as long as the compiler makes sure I'm not writing stupid code 🙂null
values