Live data from Hacker News

Should I use a Swift struct or a class?

faq.sealedabstract.com

51–56 of 56 posts

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

#51
post #38
post #27

Earlier quoted context omitted.

OK, I can appreciate your stance, but can you explain the difference? (Or at least point to a reference that does?)

It comes down to equality. Values have "extensional equality" which means that values A and B are equal exactly and only when they look the same and can be used in the same way. On the other hand, most people are used to "referential equality" which is more strict. It lets me distinguish "your copy" of A from "my copy" of A via their names. Without referential equality things like mutation fail to have any sense, but…

Cool, thanks for the distinction! Gives me some terms to look up for more info, too.

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

#53
post #29
post #25

Earlier quoted context omitted.

"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 fu…

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…

I guess I'll ask this here, since you seem to know what you're talking about. I'm a cs major, and I just finished my first c class. I quite enjoyed getting my feet wet with the lower level aspects of c, c doesn't hold your hand much (compared to java or python at least) and I liked that you really have to think about what you want, and the best way to get it with c.

Do you have any books or exercises to recommend to help further my understanding of low-level computing? I'll be taking too and compilers in the fall, but I'd like to keep digging on my own.

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

#54
post #2

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

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

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.

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

#55

Earlier quoted context omitted.

Ok thanks. Looking at this from a Lisp programmer's perspective values are by definition immutable. The Swift ecosystem seems very complex.

Variables can be mutated in Lisp. That's all this is.

Variables are not values.

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

#56

Earlier quoted context omitted.

Variables can be mutated in Lisp. That's all this is.

Variables are not values.

Thr point is, this is a matter of definition, in that people often use the word "value" where "variable" or "object" could be used, especially as to distinguish from references. This is common usage in other languages too. It has nothing to do with Swift being complex.
Post reply on HN