{"id":15325,"date":"2016-07-24T15:25:27","date_gmt":"2016-07-24T19:25:27","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=15325"},"modified":"2016-07-24T15:25:27","modified_gmt":"2016-07-24T19:25:27","slug":"bridging-existentials-generics-in-swift-2","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2016\/07\/24\/bridging-existentials-generics-in-swift-2\/","title":{"rendered":"Bridging Existentials &#038; Generics in Swift 2"},"content":{"rendered":"<p><a href=\"http:\/\/blog.benjamin-encz.de\/post\/bridging-existentials-generics-swift-2\/\">Benjamin Encz<\/a>:<\/p>\n<blockquote cite=\"http:\/\/blog.benjamin-encz.de\/post\/bridging-existentials-generics-swift-2\/\">\n<p>In an <a href=\"http:\/\/blog.benjamin-encz.de\/post\/compile-time-vs-runtime-type-checking-swift\/\">earlier blog post<\/a> I pointed out some incompatibilities between type information that is statically known at compile time (Generics) and type information that is dynamically available at runtime (Existentials).<\/p>\n<p>[&#8230;]<\/p>\n<p>Given a heterogenous list of different instances that can be persisted we want to automatically find &amp; call the DAO based on the type of object we encounter.<\/p>\n<p>[&#8230;]<\/p>\n<p>The <code>.Self<\/code> member, which would refer to the concrete type of the existential doesn&rsquo;t exist in Swift 2. However, we can access the concrete type of the existential using <code>Self<\/code> from within protocols &amp; protocol extensions.<\/p>\n<p>Using a clever inversion of control we can use that <code>Self<\/code> type from within the <code>PersistedType<\/code> protocol (which all persisted types implement) to dynamically specify the generic type parameter of our <code>GenericDAO&lt;T&gt;<\/code>:<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Benjamin Encz: In an earlier blog post I pointed out some incompatibilities between type information that is statically known at compile time (Generics) and type information that is dynamically available at runtime (Existentials). [&#8230;] Given a heterogenous list of different instances that can be persisted we want to automatically find &amp; call the DAO based [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","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-15325","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\/15325","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=15325"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/15325\/revisions"}],"predecessor-version":[{"id":15326,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/15325\/revisions\/15326"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=15325"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=15325"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=15325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}