5 ms·
Should I use a Swift struct or a class?
- kaonashi 11y agoor: how I stopped worrying and accepted making programs that are hard to reason about
- nickbauman 11y agoRight? I've always been amazed at how people just accept this state of affairs.
- durandal1 11y agoIf you want to contribute, write why you disagree instead of attacking the author as a person.
- kaonashi 11y agoWasn't trying to attack; sometimes trying to create a perfect mathematical model for your program can get in the way of having a program at all.
- zephyz 11y agoThis post is basically saying "I don't understand programming and I won't even try"
- chowells 11y agoFunctional 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.
- grabcocque 11y agoIn 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.
- oldmanjay 11y ago3) synchronize correctly, which is fraught in nearly all languages. Rust has some very nice facilities for this, however, that make shared mutable state very usable indeed.
- seanmcdirmid 11y agoIf you don't have shared mutable state or shared resources, then you don't really have concurrency. Concurrency is actually defined as this problem, as opposed to parallelism which is just about splitting up work.
- grabcocque 11y agoOne thing I've learned, if you want to convince people you're right, best to insult them and tell them they're "on crack". Guaranteed to work.
- mikeash 11y agoThere'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.
- eropple 11y agoAgreed. 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.
- MichaelGG 11y agoIn C#, there's a lot more issues though with val vs ref. The CLR JIT had some serious issues with structs. OTOH, there still is no way to stackalloc reference types, so in many cases you're forced to use structs so you can get decent performance. But I've zero formal training, and I've never found val vs ref particularly confusing. Though I did wonder why VB needed ByVal at all; couldn't it all be done ByRef? But wow, this post... I've no idea how he came to the conclusion structs=functional. He just sorta dives into it? If you aren't sure on something, why not read up a bit more? Guess C is a really functional language since it's got structs everywhere.
- dfkf 11y agoThere is no way to allocate objects of reference types on stack because references can easily outlive objects, leaving you with dangling references. And as I understand ByVal in VB.NET is redundant since it's the default option. It's just a leftover from VB6 where the default was ByRef. Crazy language.
- nickbauman 11y agoIf 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.
- tel 11y agoMonads aren't nouns. When you build a state transformer you can choose whether or not to recognize that it is a monad (or maybe strong profunctor, depends upon your design) but it always just IS one.
- oulipo 11y agoNot 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
- alexjarvis 11y agoWhat a stupid, long blog post
- alexjarvis 11y agoAlso why no mention for the enum, the other value type that allows for pattern matching?
- deleted 11y ago[deleted]
- lyinsteve 11y agoSurprisingly salient and cogent points for an article that's so hostile and willfully ignorant. The author is correct about when to use both, and I'm glad he doesn't discount structs entirely like I've seen from some OOP strongholds. "The way they write functional programs for decidedly non-functional problems is through a trick called a monad, which I will not explain and nobody understands anyway," Maybe you'd be less hostile if you actually took the time to learn about generic abstractions and how they can greatly simplify a codebase. Hint: Optional is a monad, and you can use it as such.
- josteink 11y agoSo ios devs, who supposedly need to write in a memory unsafe language because that's just better, don't understand the basic difference between pass by reference and pass by value. Nice. What can possibly go wrong? Looking forward to seeing the questions in the swift SO site.
- fuckedapanda 11y agostructs are the female version of classes in swift..
- WoodenChair 11y agoSeems like a pretty good article, but as a blog, his site is violating the "put a date" principle: http://www.observationalhazard.com/2014/09/put-date-on-your-blog-post.html http://www.observationalhazard.com/2014/09/put-date-on-your-...
- MakeMake 11y agoAh, an excuse why two almost identical language constructs exist. Welcome to the list of cluttered programing languages.