i guess i should know this from java, but i always...
# getting-started
h
i guess i should know this from java, but i always just make sure by programming around the question, and figured I'd finally ask:
Copy code
val data = aDataClassOrSomething(foo = ArrayList<Stuff>())
val bar = data.foo
is
bar
passed by reference or just value? If I alter
bar
does it alter
data.foo
?
s
have you tried? lol
Copy code
data class ADataClassOrSomething<T> (val foo: ArrayList<T>)
data class Stuff(val field: Any)

val data = ADataClassOrSomething(foo = ArrayList<Stuff>())
val bar = data.foo

data.foo.add(Stuff("foo"))
bar.add(Stuff("bar"))

println(data.foo)
println(bar)
h
sometimes, but i've given up on figure out edge cases, because it feels very inconsistent
s
If you have an example of what you think might be an edge case/unintuitive, someone might be able to explain what’s happening
h
i run into this question so often, then give it 30 seconds of thought before deciding to just program around it once again because that's faster in this moment 😛
s
shrung
a
Afaik it's the same as in Java; always pass by value (which are references)
âž• 3