{"id":16040,"date":"2016-10-12T16:03:09","date_gmt":"2016-10-12T20:03:09","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=16040"},"modified":"2016-10-12T16:03:09","modified_gmt":"2016-10-12T20:03:09","slug":"swift-type-erased-protocol-property","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2016\/10\/12\/swift-type-erased-protocol-property\/","title":{"rendered":"Swift Type-Erased Protocol Property"},"content":{"rendered":"<p><a href=\"http:\/\/www.russbishop.net\/inception\">Russ Bishop<\/a>:<\/p>\n<blockquote cite=\"http:\/\/www.russbishop.net\/inception\">\n<p>Because <code>_AnyFancyBoxBase<\/code> adopts <code>FancyProtocol<\/code> it automatically gains an abstract type member the same way it gains a property or function. It can&rsquo;t stay abstract though, we need to bind it to <em>something<\/em>, either a specific type or a generic type parameter.<\/p>\n<p>[&#8230;]<\/p>\n<p>Now we can define a subclass of the base box; it inherits the protocol conformance and we provide trampoline functions that forward everything to the instance in <code>var base: Base<\/code>.<\/p>\n<p>[&#8230;]<\/p>\n<p>This type&rsquo;s raison d&rsquo;&ecirc;tre is in the very first line where we link <code>Base.Thing<\/code> (the associated type we got from the protocol) to <code>AnyFancyBoxBase.T<\/code> (the generic parameter on our base class).<\/p>\n<p>[&#8230;]<\/p>\n<p>Now we can create our type erasing wrapper. The type it is erasing is the concrete type that adopted <code>FancyProtocol<\/code> in the first place.<\/p>\n<p>[&#8230;]<\/p>\n<p>If we tried to pull this trick with one less type we would end up adding a generic parameter somewhere.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Russ Bishop: Because _AnyFancyBoxBase adopts FancyProtocol it automatically gains an abstract type member the same way it gains a property or function. It can&rsquo;t stay abstract though, we need to bind it to something, either a specific type or a generic type parameter. [&#8230;] Now we can define a subclass of the base box; it [&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":[71,901],"class_list":["post-16040","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/16040","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=16040"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/16040\/revisions"}],"predecessor-version":[{"id":16041,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/16040\/revisions\/16041"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=16040"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=16040"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=16040"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}