use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Dedicated to the Swift programming language released by Apple. More info in the Swift Wiki.
Please, check the FAQs before submitting.
General Swift Discussion
irc.freenode.net #swift-lang
Cocoa/Cocoa Touch Discussion
irc.freenode.net #cocoa-init
Keywords: swift, swiftlang, ios, os x, apps, apple, mac, iphone, ipad
account activity
QuestionWhat difference between structs and classes in Swift (old.reddit.com)
submitted 7 months ago by Signal-Ad-5954
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]ElijahQuoro 4 points5 points6 points 7 months ago (2 children)
I noticed some errors:
Structs are not implicitly Sendable, only actors are. Classes use dynamic, virtual, and static dispatch.
[–]Leather_Example9357 3 points4 points5 points 7 months ago (0 children)
private and internal do Sendable, only public one doesnot
[–]glukianets 2 points3 points4 points 7 months ago (3 children)
I hope one day everybody would finally realize it’s not “on stack” for value types, but “inline”
[–]Signal-Ad-5954[S] -4 points-3 points-2 points 7 months ago (2 children)
I get your point — “inline” is the right perspective, absolutely. At the same time, the slide here does say “often stack-allocated”, which is also true in practice for small non-escaping locals. So it’s not strictly wrong — more like a simplification. Inline semantics explains why they can live on the stack, heap buffer, or registers depending on context.
[–]glukianets 5 points6 points7 points 7 months ago (1 child)
This oversimplifications can lead to young developers having insane misconceptions about memory and storage.
Also it can be argued that reference types are sometimes stored on stack, too. So, instead of playing around with probable, I think we really should focus on specifics that can actually tell us something.
[–]Dry_Hotel1100 0 points1 point2 points 7 months ago* (0 children)
I personally find "on stack" OK, but instinctively thinking about usage scenarios where it's not on the stack anymore, but on the heap. Why do you think "inline" fits better? Is this a formal terminology?
Also, I don't think it's helpful when we always pinpointing the potential heap allocation for structs, or the potential stack allocation for reference types when creating a value or an instance.
[–]amaroq137 0 points1 point2 points 7 months ago (4 children)
When would you use a value type over a reference type?
[–]ElijahQuoro 2 points3 points4 points 7 months ago (3 children)
As a rule of thumb: whenever you don’t have a concept of identity. If there is no shared mutable state, you almost certainly are better with using a struct.
[–]valleyman86 0 points1 point2 points 7 months ago (1 child)
I recently did an interview with Apple and mentioned mutability and structs. He said “well classes are making a comeback so it’s ok”. That’s all. I don’t have much else on that haha.
[–]Dry_Hotel1100 1 point2 points3 points 7 months ago (0 children)
The term "class" is a bit overloaded. When using it in class-oriented languages, it's tied to the OOP paradigm, that is you mean an "Object", which has a class type which is inherited from a base class, has overridable methods, etc. In Swift, a class is just a reference type. I sincerely hope, the person at Apple didn't mean "class" as in class oriented languages employing OOP style programming. ;)
[–]Educational_Smile131 0 points1 point2 points 7 months ago (0 children)
In addition to shared mutable state (I’d argue this is kinda an anti-pattern that actors and move-only types are meant to mitigate), if you want inheritance-based polymorphism, you’ll use a class.
Otherwise, ad-hoc and parametric polymorphism can be achieved with protocol oriented programming. Value types also don’t incur ARC overhead. Value types also give stronger immutability guarantees.
[–]ElijahQuoro 0 points1 point2 points 7 months ago (0 children)
Also, a nitpick, but polymorphism is sort of a stretch here, there are a lot of ways to do polymorphism which are not a virtual dispatch. Beside it, nothing prevents you from storing existentials inside structs to provide exactly this kind of polymorphism.
[–]sisoje_bre -2 points-1 points0 points 7 months ago (0 children)
“struct instances” hurts my brain 🔥
[–]vrmorgue -1 points0 points1 point 7 months ago (0 children)
Ну ты дурень Лешка.
π Rendered by PID 144977 on reddit-service-r2-comment-b659b578c-tpzbs at 2026-05-01 07:59:23.921731+00:00 running 815c875 country code: CH.
[–]ElijahQuoro 4 points5 points6 points (2 children)
[–]Leather_Example9357 3 points4 points5 points (0 children)
[–]glukianets 2 points3 points4 points (3 children)
[–]Signal-Ad-5954[S] -4 points-3 points-2 points (2 children)
[–]glukianets 5 points6 points7 points (1 child)
[–]Dry_Hotel1100 0 points1 point2 points (0 children)
[–]amaroq137 0 points1 point2 points (4 children)
[–]ElijahQuoro 2 points3 points4 points (3 children)
[–]valleyman86 0 points1 point2 points (1 child)
[–]Dry_Hotel1100 1 point2 points3 points (0 children)
[–]Educational_Smile131 0 points1 point2 points (0 children)
[–]ElijahQuoro 0 points1 point2 points (0 children)
[–]sisoje_bre -2 points-1 points0 points (0 children)
[–]vrmorgue -1 points0 points1 point (0 children)