Should I use a Swift struct or a class?
faq.sealedabstract.com
Should I use a Swift struct or a class?
1–10 of 56 posts
Re: Should I use a Swift struct or a class?
#2Re: Should I use a Swift struct or a class?
#3Re: Should I use a Swift struct or a class?
#4State changes are fine! They just aren't allowed to be hidden.
Despite all that - I agree that move semantics are weird. He's got a point there.
Re: Should I use a Swift struct or a class?
#5Guaranteed to work.
Re: Should I use a Swift struct or a class?
#6Functional programming isn't about eliminating state. You can't write programs without state. Functional programming is about eliminating hidden state changes. State changes are fine! They just aren't allowed to be hidden. Despite all that - I agree that move semantics are weird . He's got a point there.
1) Make it not mutable 2) Make it not shared
But these are still "state", so I'm not really sure what the hell the article is on about.
Re: Should I use a Swift struct or a class?
#7Use 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 with ad hoc rules and guidelines that will let them decide without understanding the actual differences.
Re: Should I use a Swift struct or a class?
#8Re: Should I use a Swift struct or a class?
#9You should not use an embedded class in a struct if it is not a singleton, as in the UIBezierPath example, where when you copy the struct, you expect to do a copy-on-write of the class to avoid mutating all copies in all structs
Re: Should I use a Swift struct or a class?
#10There'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…
It worries me, to a degree, because there's probably no way to make a programming language smart enough to protect users from these people.