<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"><channel><title>mikeash.com pyblog/friday-qa-2015-07-17-when-to-use-swift-structs-and-classes.html comments</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2015-07-17-when-to-use-swift-structs-and-classes.html#comments</link><description>mikeash.com Recent Comments</description><lastBuildDate>Mon, 11 May 2026 23:37:49 GMT</lastBuildDate><generator>PyRSS2Gen-1.0.0</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Tim - 2016-09-08 21:49:11</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2015-07-17-when-to-use-swift-structs-and-classes.html#comments</link><description>Alamaprabhu: "Looks like Structs are faster in terms of execution time."
&lt;br /&gt;
&lt;br /&gt;Faster at what?  They do different things, so you can't just replace one with the other.
&lt;br /&gt;
&lt;br /&gt;It's like asking if a binary tree is faster or slower than a hash table.  At some things, it's faster, and at other things, it's slower, but you'd never replace one with the other based on hearing "data structure X is faster than data structure Y".  You pick the appropriate one for the semantics you need.</description><guid isPermaLink="true">f04d97dfa2496e360a1e5b2bb41305ce</guid><pubDate>Thu, 08 Sep 2016 21:49:11 GMT</pubDate></item><item><title>Edo Pelawi - 2016-06-14 07:05:14</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2015-07-17-when-to-use-swift-structs-and-classes.html#comments</link><description>&lt;div class="blogcommentquote"&gt;&lt;div class="blogcommentquoteinner"&gt;
&lt;br /&gt;Finally, beware of embedding reference types in value types, as terrible things can happen if you're not careful.
&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;Interesting statement. I'm currently considering using RxSwift's Variables inside my Struct, and reading this made me realised that it fits this warning.
&lt;br /&gt;
&lt;br /&gt;Do you have any references or documentations regarding value types owning reference types? Thanks in advance! :)</description><guid isPermaLink="true">70bda051e0d479c8c59e4bca3fd679ad</guid><pubDate>Tue, 14 Jun 2016 07:05:14 GMT</pubDate></item><item><title>Allamaprabhu - 2015-09-08 06:16:59</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2015-07-17-when-to-use-swift-structs-and-classes.html#comments</link><description>Looks like Structs are faster in terms of execution time.Can you please explain more about that?.I feel its important decision to be considered while choosing types in swift.</description><guid isPermaLink="true">dc51f2080471733c50a9bbe23a3f4e8b</guid><pubDate>Tue, 08 Sep 2015 06:16:59 GMT</pubDate></item><item><title>Robert Payne - 2015-08-18 10:03:12</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2015-07-17-when-to-use-swift-structs-and-classes.html#comments</link><description>I may be wrong, but I believe reading somewhere that when Swift structs are copied that reference Objective-C values there could also be a huge hit on ARC semantics required to safely copy it (increment retain counts for each referenced value in the struct).
&lt;br /&gt;
&lt;br /&gt;Wonder if anyone has dug in on that.</description><guid isPermaLink="true">44a7d81161f166b24fa54377b89dbf01</guid><pubDate>Tue, 18 Aug 2015 10:03:12 GMT</pubDate></item><item><title>austinz - 2015-07-29 08:52:11</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2015-07-17-when-to-use-swift-structs-and-classes.html#comments</link><description>@Patrick: That's a cool idea, but you can have variables of (non-'class') protocol type whose semantics differ based on whether they're assigned struct or class instances.
&lt;br /&gt;
&lt;br /&gt;In that case, the compiler can't tell at compile time whether a variable is going to have value or reference semantics, which means you'd have to tolerate your assignment symbol being misleading, or pick a third one for protocol types.</description><guid isPermaLink="true">28fe728dfc08a3b243ad5179db914bdf</guid><pubDate>Wed, 29 Jul 2015 08:52:11 GMT</pubDate></item><item><title>Patrick Weigel - 2015-07-25 02:38:21</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2015-07-17-when-to-use-swift-structs-and-classes.html#comments</link><description>So it finally hit me - since value type and reference type assignment are fundamentally different, Swift should use a different symbol for value type assignment and reference type assignment.
&lt;br /&gt;
&lt;br /&gt;I have a suggestion below for the exact symbol to use in each case, but am open to other ideas. What do you think?
&lt;br /&gt;
&lt;br /&gt;&amp;lt;-    Use this for value type assignment
&lt;br /&gt;&amp;lt;•    Use this for reference type assignment (• is option-8)
&lt;br /&gt;
&lt;br /&gt;The compiler will know what assignment type each assignment is, so can return an error when you use the wrong one.
&lt;br /&gt;
&lt;br /&gt;In the examples in the above post:
&lt;br /&gt;SomeClass *reference2 &amp;lt;• reference; // reference assignment
&lt;br /&gt;value.number &amp;lt;- 43;  // value assignment
&lt;br /&gt;SomeClass *reference2 &amp;lt;- reference; // compiler error
&lt;br /&gt;value.number &amp;lt;• 43;   // compiler error
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</description><guid isPermaLink="true">29c056732edc3c273e6c9233137d6ad4</guid><pubDate>Sat, 25 Jul 2015 02:38:21 GMT</pubDate></item><item><title>Jonathan - 2015-07-24 14:52:52</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2015-07-17-when-to-use-swift-structs-and-classes.html#comments</link><description>Great article. As the "using a reference type inside a value type" is  useful, but contains a potentially huge gotcha, do you think it should  be something there is a compiler warning about? Or even an annotation so that the compiler (and the reader) know this was deliberate ?
&lt;br /&gt;
&lt;br /&gt;I'm thinking of the poor developer who accidentally uses NSString instead of String...</description><guid isPermaLink="true">d4dc99003f77690384c35b3c6bf2c3fb</guid><pubDate>Fri, 24 Jul 2015 14:52:52 GMT</pubDate></item><item><title>Michael M. Mayer - 2015-07-20 04:44:45</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2015-07-17-when-to-use-swift-structs-and-classes.html#comments</link><description>Another great article and a wonderful complement to the Value Semantics WWDC session (414).  This article was made doubly good by the reference to "tagged pointers" of which I had never heard.  You've have filled a hole in my professional knowledge I never even knew existed, but for which I am deeply glad to have obtained.</description><guid isPermaLink="true">7debc500c6c1160929ff469373549676</guid><pubDate>Mon, 20 Jul 2015 04:44:45 GMT</pubDate></item></channel></rss>
