{"id":11921,"date":"2015-08-03T10:01:18","date_gmt":"2015-08-03T14:01:18","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=11921"},"modified":"2015-08-05T09:47:12","modified_gmt":"2015-08-05T13:47:12","slug":"swift-protocols-arrays-and-casting","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/08\/03\/swift-protocols-arrays-and-casting\/","title":{"rendered":"Swift Protocols, Arrays, and Casting"},"content":{"rendered":"<p><a href=\"http:\/\/inessential.com\/2015\/08\/02\/swift_diary_7_protocols_arrays_and_c\">Brent Simmons<\/a> (<a href=\"https:\/\/twitter.com\/brentsimmons\/status\/627994188584321024\">tweet<\/a>):<\/p>\r\n<blockquote cite=\"http:\/\/inessential.com\/2015\/08\/02\/swift_diary_7_protocols_arrays_and_c\"><p>But an EmailMessage <em>is<\/em> a NodeRepresentedObject, so it ought to work just fine.<\/p>\r\n<p>[&#8230;]<\/p>\r\n<p>The best way I&rsquo;ve found to deal with this is to map it. [&#8230;] That seems crazy. I&rsquo;m using map to create an array whose members are absolutely identical to the original array.<\/p><\/blockquote>\r\n\r\n<p><a href=\"https:\/\/twitter.com\/eridius\/status\/628000848191205376\">Kevin<\/a> <a href=\"https:\/\/twitter.com\/eridius\/status\/628001009533476864\">Ballard<\/a>:<\/p>\r\n<blockquote cite=\"https:\/\/twitter.com\/eridius\/status\/628000848191205376\"><p>It&rsquo;s not a type-check. The in-memory representation is different. It has to allocate a new array with the new values.<\/p><\/blockquote>\r\n<blockquote cite=\"https:\/\/twitter.com\/eridius\/status\/628001009533476864\"><p>Non-@objc protocols use a virtual function table (called a protocol witness table) rather than message-passing.<\/p><\/blockquote>\r\n<blockquote cite=\"https:\/\/twitter.com\/eridius\/status\/628001071135264768\"><p>This means that objects typed as EmailMessage have a different table pointer than those typed as NodeRepresentedObject.<\/p><\/blockquote>\r\n<p><a href=\"https:\/\/twitter.com\/brentsimmons\/status\/628017047952211969\">Brent Simmons<\/a>:<\/p>\r\n<blockquote cite=\"https:\/\/twitter.com\/brentsimmons\/status\/628017047952211969\"><p>I&rsquo;d argue that, instead, there shouldn&rsquo;t be a hidden performance gotcha.<\/p><\/blockquote>\r\n<p><a href=\"https:\/\/twitter.com\/eridius\/status\/628017377863561216\">Kevin<\/a> <a href=\"https:\/\/twitter.com\/eridius\/status\/628018970512109568\">Ballard<\/a>:<\/p>\r\n<blockquote cite=\"https:\/\/twitter.com\/eridius\/status\/628017377863561216\"><p>That would be nice, but the only way to solve this without a new array basically requires all protocols to use message-passing<\/p><\/blockquote>\r\n<blockquote cite=\"https:\/\/twitter.com\/eridius\/status\/628018970512109568\"><p>Message passing is a performance gotcha :P<\/p><\/blockquote>\r\n<p><a href=\"https:\/\/twitter.com\/brentsimmons\/status\/628019277509955585\">Brent Simmons<\/a>:<\/p>\r\n<blockquote cite=\"https:\/\/twitter.com\/brentsimmons\/status\/628019277509955585\"><p>But barely. We can write fast apps in Objective-C.<\/p><\/blockquote>\r\n<p><a href=\"https:\/\/twitter.com\/eridius\/status\/628019348259516416\">Kevin Ballard<\/a>:<\/p>\r\n<blockquote cite=\"https:\/\/twitter.com\/eridius\/status\/628019348259516416\"><p>Yeah, but you should be able to write faster ones in Swift.<\/p><\/blockquote>\r\n<p><a href=\"https:\/\/twitter.com\/brentsimmons\/status\/628020371128610816\">Brent Simmons<\/a>:<\/p>\r\n<blockquote cite=\"https:\/\/twitter.com\/brentsimmons\/status\/628020371128610816\"><p>Except for when I have to make an exact copy of an array to satisfy the type system.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Brent Simmons (tweet): But an EmailMessage is a NodeRepresentedObject, so it ought to work just fine. [&#8230;] The best way I&rsquo;ve found to deal with this is to map it. [&#8230;] That seems crazy. I&rsquo;m using map to create an array whose members are absolutely identical to the original array. Kevin Ballard: It&rsquo;s not a [&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,54,138,71,901,943],"class_list":["post-11921","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-message-passing","tag-objective-c","tag-optimization","tag-programming","tag-swift-programming-language","tag-swift-runtime"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/11921","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=11921"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/11921\/revisions"}],"predecessor-version":[{"id":11922,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/11921\/revisions\/11922"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=11921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=11921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=11921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}