Live data from Hacker News

Should I use a Swift struct or a class?

faq.sealedabstract.com

21–30 of 56 posts

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

#21
post #17

Earlier quoted context omitted.

Values can't be mutated if they're Ivars and you're in a non-mutating func. Non-mutating funcs are unique to value semantics.

True but I'm not sure what your point is. If you need a mutating struct method then of course you have to mark it as mutating.

One does not "simply" mark a method as mutating.

A) One must ensure that one's callers are also mutating, or otherwise have an assignable reference, all the way up the stack

B) One must mark the applicable function in the protocol interface as mutating, which may or may not be under your control

C) One must now repeat step A for all consumers of the protocol in step B, even if none of the other implementations of that protocol mutate their own state

Of course one can design one's architecture specifically with a view toward making ABC trivial, but in the general, unrestricted, default, I-had-other-problems-that-day-and-didn't-think-about-it case, they are hard.

That is why I find calls to "just learn value semantics" necessary but insufficient. Value semantics will not refactor my code when I discover-by-surprise that 18 functions need to become mutating, breaking API compatibility across several build targets, because I failed to forsee a necessary mutation 8 months ago.

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

#22
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…

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.

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

#23
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…

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

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

#24
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…

[deleted]

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

#25
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…

"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

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

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

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

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

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

#28
post #25
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…

"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 issue is that most of the cocoa framework requires classes to work with it. For anything UI, you are going to be dealing with reference semantics unless apple gives us an alternative to cocoa touch like http://componentkit.org

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

#29
post #25
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…

"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 immediately know how it works...

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

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

  let a = MyStruct(x: 1, y: 2)
  let b = a // Creates a copy of `a`
  b.x = 3 // Won't compile because you used "let", so its properties are immutable
  var c = a
  c.x = 3
  println(a.x) // Prints "1"; the original instance doesn't get changed

  let d = MyClass(x: 1, y: 2)
  let e = d // Refers to the same object as `d`
  e.x = 3 // Allowed, even though you used "let"
  println(d.x) // Prints "3"
That's a basic illustration of the difference between value types and reference types, but there are other differences between structs and classes as well (for instance, structs don't support inheritance or deinitializers).
Post reply on HN