Back to questions
JuniorTheoryCommon
What happens when this program is compiled and run?
class Box {
var n = 0
}
let b = Box()
b.n = 5
print(b.n)5
- Expecting a compile error, as with a struct in a
let: for a class,letfixes only the reference, so the object'svarproperties still change. - Expecting
0, as ifb.n = 5changed a copy:bleads to the object itself, so the change stays in it.
- What happens if the program then writes
b = Box()? - What changes if
Boxis declared as astruct?