There's a ton of discussion in the Swift world about when to use structs and when to use classes. And I just don't understand why. Use structs when you want value semantics. Use classes when you want reference semantics. Boom. Done. All the confusion and discussion and fighting seems to be because people don't understand the implications of value versus reference semantics, and instead of learning they try to come up…
Should I use a Swift struct or a class?
11–20 of 56 posts
Re: Should I use a Swift struct or a class?
#12There's a ton of discussion in the Swift world about when to use structs and when to use classes. And I just don't understand why. Use structs when you want value semantics. Use classes when you want reference semantics. Boom. Done. All the confusion and discussion and fighting seems to be because people don't understand the implications of value versus reference semantics, and instead of learning they try to come up…
Warning: I haven't learned Swift yet. So let's say you want to use value semantics so you choose a struct. But later you find that you have to change state of the values. Is your only resort then to switch to using classes? Because if that's true that's a false choice.
Re: Should I use a Swift struct or a class?
#13or: how I stopped worrying and accepted making programs that are hard to reason about
Re: Should I use a Swift struct or a class?
#14Re: Should I use a Swift struct or a class?
#15Re: Should I use a Swift struct or a class?
#16Earlier quoted context omitted.
Warning: I haven't learned Swift yet. So let's say you want to use value semantics so you choose a struct. But later you find that you have to change state of the values. Is your only resort then to switch to using classes? Because if that's true that's a false choice.
Values can still be mutated. Value versus reference isn't about mutable versus immutable. It's about whether when you do a = b you get a new instance or just a new reference to the same instance.
Re: Should I use a Swift struct or a class?
#17Earlier quoted context omitted.
Values can still be mutated. Value versus reference isn't about mutable versus immutable. It's about whether when you do a = b you get a new instance or just a new reference to the same instance.
Values can't be mutated if they're Ivars and you're in a non-mutating func. Non-mutating funcs are unique to value semantics.
Re: Should I use a Swift struct or a class?
#18Earlier quoted context omitted.
Warning: I haven't learned Swift yet. So let's say you want to use value semantics so you choose a struct. But later you find that you have to change state of the values. Is your only resort then to switch to using classes? Because if that's true that's a false choice.
Values can still be mutated. Value versus reference isn't about mutable versus immutable. It's about whether when you do a = b you get a new instance or just a new reference to the same instance.
Re: Should I use a Swift struct or a class?
#19Earlier quoted context omitted.
Values can't be mutated if they're Ivars and you're in a non-mutating func. Non-mutating funcs are unique to value semantics.
True but I'm not sure what your point is. If you need a mutating struct method then of course you have to mark it as mutating.