{"id":16567,"date":"2016-12-08T14:54:18","date_gmt":"2016-12-08T19:54:18","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=16567"},"modified":"2016-12-10T09:39:27","modified_gmt":"2016-12-10T14:39:27","slug":"method-dispatch-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2016\/12\/08\/method-dispatch-in-swift\/","title":{"rendered":"Method Dispatch in Swift"},"content":{"rendered":"<p><a href=\"https:\/\/www.raizlabs.com\/dev\/2016\/12\/swift-method-dispatch\/\">Brian King<\/a>:<\/p>\n<blockquote cite=\"https:\/\/www.raizlabs.com\/dev\/2016\/12\/swift-method-dispatch\/\">\n<p>Compiled programming languages have three primary methods of dispatch at their disposal: direct dispatch, table dispatch, and message dispatch, which I explain below. Most languages support one or two of these. Java uses table dispatch by default, but you can opt into direct dispatch by using the <code>final<\/code> keyword. C++ uses direct dispatch by default, but you can opt into table dispatch by adding the <code>virtual<\/code> keyword. Objective-C always uses message dispatch, but allows developers to fall back to C in order to get the performance gains of direct dispatch. Swift has taken on the noble goal of supporting all three types of dispatch. This works remarkably well, but is a source of confusion to many developers, and is behind a number of gotchas that most Swift developers have encountered.<\/p>\n<p>[&#8230;]<\/p>\n<p>So, how does Swift dispatch methods? I haven&rsquo;t found a succinct answer to this question, but here are four aspects that guide how dispatch is selected:<\/p>\n<ul>\n<li>Declaration Location<\/li>\n<li>Reference Type<\/li>\n<li>Specified Behavior<\/li>\n<li>Visibility Optimizations<\/li>\n<\/ul>\n<p>[&#8230;]<\/p>\n<p>Above, I mentioned that methods defined inside the initial declaration of an <code>NSObject<\/code> subclass use table dispatch. I find this to be confusing, hard to explain, and in the end, it&rsquo;s only a marginal performance improvement.<\/p>\n<\/blockquote>\n<p>Swift extensions use direct dispatch (less dynamic than in the class declaration), while <code>NSObject<\/code> extensions use message dispatch (more dynamic than in the class declaration). Then he gives an example where an <code>NSObject<\/code> override method is not called because it&rsquo;s in an extension rather than directly in the subclass; the location of the <em>initial<\/em> declaration matters. Also, you can override <code>NSObject<\/code> extension methods but not Swift ones. And don&rsquo;t forget <a href=\"http:\/\/mjtsai.com\/blog\/2015\/09\/01\/swift-protocols\/\">protocols<\/a>. It&rsquo;s all rather confusing.<\/p>\n<p>Update (2016-12-10): See also: <a href=\"https:\/\/twitter.com\/an0\/status\/807267509699309570\">Ling Wang<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Brian King: Compiled programming languages have three primary methods of dispatch at their disposal: direct dispatch, table dispatch, and message dispatch, which I explain below. Most languages support one or two of these. Java uses table dispatch by default, but you can opt into direct dispatch by using the final keyword. C++ uses direct dispatch [&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,966,71,901],"class_list":["post-16567","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-message-passing","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/16567","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=16567"}],"version-history":[{"count":3,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/16567\/revisions"}],"predecessor-version":[{"id":16589,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/16567\/revisions\/16589"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=16567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=16567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=16567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}