{"id":13405,"date":"2016-02-02T14:36:41","date_gmt":"2016-02-02T19:36:41","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=13405"},"modified":"2016-02-02T14:36:41","modified_gmt":"2016-02-02T19:36:41","slug":"seven-swift-snares","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2016\/02\/02\/seven-swift-snares\/","title":{"rendered":"Seven Swift Snares"},"content":{"rendered":"<p><a href=\"https:\/\/developer.ibm.com\/swift\/2016\/01\/27\/seven-swift-snares-how-to-avoid-them\/\">David Ungar<\/a> (<a href=\"https:\/\/news.ycombinator.com\/item?id=10982558\">comments<\/a>):<\/p>\n<blockquote cite=\"https:\/\/developer.ibm.com\/swift\/2016\/01\/27\/seven-swift-snares-how-to-avoid-them\/\">\n<p>But with value types, there&rsquo;s no <code>override<\/code> keyword to help the compiler find my mistakes. This omission seems out of place in a language otherwise designed to include enough redundancy to help find one&rsquo;s errors.<\/p>\n<p>[&#8230;]<\/p>\n<p>All that can be known at compile-time is that pie is a <code>Pizza<\/code>, and the <code>Pizza<\/code> protocol extension says Wheat, so the declaration of a cornmeal crust in the <code>CornmealPizza<\/code> structure had no effect whatsoever when asking pie for something. Although the compiler could have warned about the potential for error from this use of static- instead of dynamic-dispatch, it did not. I believe that here lurks a trap for the unwary, and I would call this a <strong>major snare<\/strong>.<\/p>\n<p>[&#8230;]<\/p>\n<p>Presenting Swift with both a declaration and a definition in this fashion causes the compiler to take notice of the runtime value of the pie variable.<\/p>\n<p>[&#8230;]<\/p>\n<p>However, without editing the source code in the framework, we cannot fix this problem. Hence, it is impossible to safely extend a protocol declared in another framework (without gambling that it will never need dynamic-dispatch.)<\/p>\n<p>[&#8230;]<\/p>\n<p>As shown in a previous section, a declaration in a protocol was sufficient to induce dynamic dispatch for a <em>defined<\/em> of the corresponding attribute in the protocol extension. But a definition in a <em>restricted<\/em> extension is <em>always statically-dispatched<\/em>.<\/p>\n<p>[&#8230;]<\/p>\n<p>Avoid assigning the result of an expression with side-effects to left-hand-side with optional chaining.<\/p>\n<p>[&#8230;]<\/p>\n<p>In-out parameters do not work when passed into the outer scope of a closure<\/p>\n<\/blockquote>\n\n<p><a href=\"https:\/\/twitter.com\/jckarter\/status\/693088224785223680\">Joe Groff<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/jckarter\/status\/693088224785223680\"><p>These are all things we hope to remove or provide diagnostics for in 3.0, at least.<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/twitter.com\/staypufd\/status\/693119560581410816\">Sam Griffith<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/staypufd\/status\/693119560581410816\"><p>Swift for all its newness is going down a path much like C++ did as far as complexity.  That is worrisome.<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/twitter.com\/orta\/status\/693091042497806337\">&Oslash;rta<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/orta\/status\/693091042497806337\"><p>Swift&rsquo;s init rules are confusing. I&rsquo;ve never once got anything non-trivial right first time<\/p><\/blockquote>\n\n<p><a href=\"http:\/\/blog.metaobject.com\/2014\/06\/remove-features-for-greater-power-aka.html\">Marcel Weiher<\/a> (<a href=\"https:\/\/twitter.com\/mpweiher\/status\/693122774563426304\">tweet<\/a>, first linked <a href=\"http:\/\/mjtsai.com\/blog\/2014\/06\/04\/swift-links\/\">in 2014<\/a>):<\/p>\n<blockquote cite=\"http:\/\/blog.metaobject.com\/2014\/06\/remove-features-for-greater-power-aka.html\"><p>Apple&rsquo;s new Swift language has taken a page from the C++ and Java \nplaybooks and made initialization a special case.  Well, <em>lots<\/em>\nof special cases actually.  The Swift book has <em>30 pages<\/em> on\ninitialization, and they aren&rsquo;t just illustration and explanation,\nthey are dense with rules and special cases.<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/twitter.com\/jckarter\/status\/693123488752672768\">Joe<\/a> <a href=\"https:\/\/twitter.com\/jckarter\/status\/693130511300997120\">Groff<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/jckarter\/status\/693123488752672768\"><p>The initializer model does need reconsideration, though. Current model doesn&rsquo;t fit our ABI resilience goals.<\/p><\/blockquote>\n<blockquote cite=\"https:\/\/twitter.com\/jckarter\/status\/693130511300997120\"><p>A little up-front complexity for initializers greatly simplifies the state space for everything else.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>David Ungar (comments): But with value types, there&rsquo;s no override keyword to help the compiler find my mistakes. This omission seems out of place in a language otherwise designed to include enough redundancy to help find one&rsquo;s errors. [&#8230;] All that can be known at compile-time is that pie is a Pizza, and the Pizza [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[46,71,901],"class_list":["post-13405","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/13405","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=13405"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/13405\/revisions"}],"predecessor-version":[{"id":13406,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/13405\/revisions\/13406"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=13405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=13405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=13405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}