Hacker News new | ask | show | jobs
by holyjaw 3998 days ago
OK, I can appreciate your stance, but can you explain the difference? (Or at least point to a reference that does?)
2 comments

  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).
Nice example, thanks!
It comes down to equality. Values have "extensional equality" which means that values A and B are equal exactly and only when they look the same and can be used in the same way. On the other hand, most people are used to "referential equality" which is more strict. It lets me distinguish "your copy" of A from "my copy" of A via their names.

Without referential equality things like mutation fail to have any sense, but programs are in general simpler.

Cool, thanks for the distinction! Gives me some terms to look up for more info, too.