Live data from Hacker News

Should I use a Swift struct or a class?

faq.sealedabstract.com

1–10 of 56 posts

Re: Should I use a Swift struct or a class?

#4
Functional 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.

Re: Should I use a Swift struct or a class?

#6
post #4

Functional 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.

In the presence of concurrency, shared mutable state is a disaster. There are two ways to fix this:

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?

#7
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 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?

#8
If your understanding of functional programming regarding state management is monads, I'd argue you don't understand. Monads are the guided nuclear missile of functional state management. If you don't know why you need them you don't need them. There are simpler ways to manage state in FP.

Re: Should I use a Swift struct or a class?

#9
Not sure though why you justify the fact that you should'nt use singletons embedded in a struct because they are copied by reference? If it is a singleton, you want it to be copied by reference. And you actually expect it to be since you want all the structs to refer to the same object?

You 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?

#10
post #7

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…

Agreed. You see the same vague cargo-culting in C#. The popularity of both (as the de-facto standards on Windows and OS X/iOS) has just led to a lot of people who've learned enough to "get things done" rather than understanding both practical and theoretical underpinnings of the systems that they're using.

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.

Post reply on HN