Structs vs Classes?Ask Questions

 Posted on 28 days ago

Other than pass by value vs pass by reference, what are the differences between structs and classes?

Also, are there any cases where using one would be more advantageous than the other, and if so, when?

Share On: facebook gplus twitter
profile
Asked by Soniya Disoza on 28 days ago Score: 20 points
Add Comment:

Comments

1 Answers

0 Corrected Answers
Aproved Answers
1
Profile
Answered by Helder Sepulveda on 10/24/2019 7:52:39 AM Score: 58 points

Other than pass by value vs pass by reference, what are the differences between structs and classes?

I wouldn’t consider the way that parameters are passed to be a key difference between structs are classes.  Swift will happily pass a struct by reference if it can prove that doing so doesn’t break the struct’s semantics.  Critically, structs (along with enums) are expected to exhibit value semantics, while clases exhibit reference semantics, per KMT’s explanation.

This distinction is trickier than you might think.  For example:

  • You can create a struct that uses a class to hold its core data, using copy on write to preserve value semantics.  Many of Swift’s standard types do this, String, Array, Data, and so on.

  • If you don’t implement copy on write properly, you can end up with struct that has reference semantics!  Don’t do that.  Anyone reading your code, including Future You™, will be very confused.

  • If you create an immutable class it effectively has value semantics (because all references are equivalent).  The Foundation framework has taken advantage of this for years (for example, NSUUID).

IMO the key difference between a struct and a class relates to identity.  If you use a class, you can maintain a persistent identity for your data over time.  This is essentially for modelling various real world constructs.

For example, consider a TCP connection.  This exists in the real world, outside of your app.  If you use a struct, what does the struct’s value semantics mean?  If you make a copy, does it ‘clone’ the TCP connection.  Such a concept is nonsense, and thus it only makes sense to use a class.

Comments

Add Comment:

Post Your Answers

Existing Members

Sign in to your account
Email Address
Password
New Member?
Sign up and complete profile
Full Name
Email Address
I have read and agree to the Terms of Service and Privacy Policy
Please subscribe me to the StoodQ newsletters
Guideline to answer a question:

Useful tips to submit your answer
Please read below guidelines before you submit your answer for question.

  • Read and understand question for which you are submitting your answer.
  • Try to avoid grammatical and spell mistake while answering.
  • Do not post any irrelevant information in your answer.
  • Explain your answer with example or any reference link to help who posted question.
  • If you find irrelevant question, please report it to support. Click here to contact support.
  • You agree to the privacy policy and terms of use to submit any contents.

Note: StoodQ is online developers community which helps developer for their difficulty, lets help them with your value contribution.