Hacker News new | ask | show | jobs
by RussianCow 3999 days ago

  let a = MyStruct(x: 1, y: 2)
  let b = a // Creates a copy of `a`
  b.x = 3 // Won't compile because you used "let", so its properties are immutable
  var c = a
  c.x = 3
  println(a.x) // Prints "1"; the original instance doesn't get changed

  let d = MyClass(x: 1, y: 2)
  let e = d // Refers to the same object as `d`
  e.x = 3 // Allowed, even though you used "let"
  println(d.x) // Prints "3"
That's a basic illustration of the difference between value types and reference types, but there are other differences between structs and classes as well (for instance, structs don't support inheritance or deinitializers).
1 comments

Nice example, thanks!