
Should I use a Swift struct or a class? - ingve
http://faq.sealedabstract.com/structs_or_classes/
======
mikeash
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.

~~~
gdubs
"And I just don't understand why."

I think you've correctly identified the source of confusion -- value vs
reference. To you, that might be plain as day. But, to many iOS devs, they
really couldn't explain the nuances.

I mean, look how much trouble beginner programmers have with the concept of
Pointers for pete's sake.

But, I think you're right -- weird rules and guidelines just confuse the
issue.

Andy Matuschak's functional swift talk puts the ref vs value discussion in
practical terms of a Khan Academy drawing app:

[http://2014.funswiftconf.com/speakers/andy.html](http://2014.funswiftconf.com/speakers/andy.html)

~~~
ajg360
The other issue is that too many programmers these days don't have a solid
computer science background... I'm all for people being self-taught and
learning programming on their own... But the downside of that prevents
individuals from learning how things work at a lower level.

I've never written a line of Obj-C or Swift in my life, but all you have to
tell me is "struct = value based, class = referenced based" and I immediately
know how it works...

~~~
ezy
I'm going to take issue with this. This has nothing to do with having a solid
computer science background -- in fact, I've seen _way_ more architecture
astronauts with CS degrees.

It has to do with learning stuff on a lower level -- whether or not you attend
a school to do so. Us autodidacts who grew up on assembly language and C code
will beg to differ on your use of the word "prevents".

~~~
WoodenChair
I presume he's not talking about long-time autodidacts. He's talking about the
many programmers who have six months of JavaScript experience from a bootcamp
and are learning Swift.

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

~~~
grabcocque
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.

~~~
oldmanjay
3) 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.

------
grabcocque
One 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.

------
zephyz
This post is basically saying "I don't understand programming and I won't even
try"

------
lyinsteve
Surprisingly 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.

------
oulipo
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

------
WoodenChair
Seems 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-...](http://www.observationalhazard.com/2014/09/put-date-on-your-blog-
post.html)

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

~~~
tel
Monads 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.

------
kaonashi
or: how I stopped worrying and accepted making programs that are hard to
reason about

~~~
durandal1
If you want to contribute, write why you disagree instead of attacking the
author as a person.

~~~
kaonashi
Wasn'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.

------
alexjarvis
Also why no mention for the enum, the other value type that allows for pattern
matching?

------
MakeMake
Ah, an excuse why two almost identical language constructs exist. Welcome to
the list of cluttered programing languages.

------
josteink
So 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.

------
alexjarvis
What a stupid, long blog post

