Live data from Hacker News

Should I use a Swift struct or a class?

faq.sealedabstract.com

31–40 of 56 posts

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

#31
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'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".

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

#32

Earlier quoted context omitted.

I think the problem is that most people coming to Swift come from languages like Ruby and JS where just about everything is a reference (or pass by value where every value is a reference anyway). The distinction between value and reference is really hard to grasp when your background is in languages that are opaque like that. I remember Go had tons of questions of this nature early on in its adoption upswing.

That's odd. You'd think most people would be coming from objc

Just the same in Objective C, unless you're dealing with a C struct (not being passed by pointer) or a primitive type you're in a sea of Objective C references. Value types are brand new to the non-Foundation framework ecosystem as far as writing new, stylistically correct, code is concerned. This in my experience does seem to be a sticking point for people without a good bit of experience in languages with the same or similar value and reference type split (C++, Java, C#, Rust, etc).

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

#33
post #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 prote…

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

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

#35
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.

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.

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

#36
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.

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

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

#37
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.

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

#38
post #27
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…

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 programs are in general simpler.

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

#39

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.

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.

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

#40
post #10

Earlier quoted context omitted.

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 prote…

In 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 wo…

There 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.
Post reply on HN