{"id":23540,"date":"2018-11-29T16:43:50","date_gmt":"2018-11-29T21:43:50","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=23540"},"modified":"2018-11-29T16:43:50","modified_gmt":"2018-11-29T21:43:50","slug":"lifting-the-self-or-associated-type-constraint-on-swift-existentials","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2018\/11\/29\/lifting-the-self-or-associated-type-constraint-on-swift-existentials\/","title":{"rendered":"Lifting the &ldquo;Self or associated type&rdquo; Constraint on Swift Existentials"},"content":{"rendered":"<p><a href=\"https:\/\/forums.swift.org\/t\/lifting-the-self-or-associated-type-constraint-on-existentials\/18025\/45\">Alexis Gallagher<\/a> (<a href=\"https:\/\/twitter.com\/alexisgallagher\/status\/1066931847538429953\">tweet<\/a>):<\/p>\n<blockquote cite=\"https:\/\/forums.swift.org\/t\/lifting-the-self-or-associated-type-constraint-on-existentials\/18025\/45\"><p>&ldquo;PAT&rdquo; is an acronym for Protocol with Associated Type. I coined the term in 2015 in a <a href=\"https:\/\/youtu.be\/XWoNjiSPqI8\">talk<\/a> at the Swift functional programming conference that year.<\/p>\n<p>That talk is a sort of a mirror of the confusion of Swift devs like myself at the time, who were excited about Swift protocols and your WWDC talk about protocol-oriented programming but also quite surprised by the limitations concerning associated types. I guessed at the motivations for this aspect of the language, relating it to other languages and to your talk. This was in the days before Swift was open-sourced, when we couldn&rsquo;t just ask about these things.<\/p>\n<p>I suspect many people, like me, are still a bit confused about the fundamental reason for these limitations.<\/p>\n<p>[&#8230;]<\/p>\n<p>Fwiw, I am sure the essential reason for the confusion then was that prototocols with associated types behaved almost exactly unlike protocols in objective-c, which was most everyone&rsquo;s reference point.<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/forums.swift.org\/t\/lifting-the-self-or-associated-type-constraint-on-existentials\/18025\/57\">Dave Abrahams<\/a>:<\/p>\n<blockquote cite=\"https:\/\/forums.swift.org\/t\/lifting-the-self-or-associated-type-constraint-on-existentials\/18025\/57\"><p>I agree that the reference point of protocols in Objective-C was probably a source of confusion. However, I think the bigger source of confusion is that we have one thing called <code>protocol<\/code> that<\/p>\n<ul>\n<li>plays two distinct roles that overlap significantly in their capabilities<\/li>\n<li>can only support a fraction of its declared API in one of its roles<\/li>\n<\/ul>\n<p>You can define the &ldquo;existential type <code>P<\/code>&rdquo; to be the most specific possible common supertype of all types conforming to the protocol <code>P<\/code>. [&#8230;]<\/p>\n<p>As for the fundamental reasons for this difference, it&rsquo;s what I said in the talk: capturing an instance of type T as an existential type erases type information, and in particular, type relationships.  If you work through a couple of examples with a protocol like [&#8230;] you&rsquo;ll see that the most specific common supertype of types conforming to <code>P<\/code> has no usable API.  The compiler can&rsquo;t provide a working <code>init()<\/code> because it has no way to know which subtype to create.<\/p>\n<p>[&#8230;]<\/p>\n<p>I loathe writing type-erasing wrappers as much as the next guy. But generalized existentials don&rsquo;t eliminate the need to write them, nor (AFAICS) do they make it much easier to do so.<\/p>\n<p>[&#8230;]<\/p>\n<p>Nobody has proposed a set of features really targeted at the pain points you cite; that would be a really interesting exploration (I suspect the answer has at least as much to do with solving what I call &ldquo;the API forwarding problem&rdquo; as it does with existentials). Even if what <em>is<\/em> being proposed makes incremental progress toward solving those problems, though, I fear that in the specific place we arrive, having applied that increment, we&rsquo;ll have done more harm than good as explained in my opening message.<\/p>\n<\/blockquote>\n\n<p>Previously: <a href=\"https:\/\/mjtsai.com\/blog\/2016\/02\/19\/swift-protocols-with-associated-types\/\">Swift Protocols With Associated Types<\/a>, <a href=\"https:\/\/mjtsai.com\/blog\/2018\/02\/28\/swift-protocols-wishlist\/\">Swift Protocols Wishlist<\/a>, <a href=\"https:\/\/mjtsai.com\/blog\/2017\/12\/20\/patterns-for-working-with-associated-types\/\">Patterns for Working With Associated Types<\/a>, <a href=\"https:\/\/mjtsai.com\/blog\/2016\/06\/01\/swift-type-erasure\/\">Swift Type Erasure<\/a>.<\/p>\n\n<p>See also: <a href=\"http:\/\/chris.eidhof.nl\/post\/type-erasers-in-swift\/\">Type Erasers in Swift<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Alexis Gallagher (tweet): &ldquo;PAT&rdquo; is an acronym for Protocol with Associated Type. I coined the term in 2015 in a talk at the Swift functional programming conference that year. That talk is a sort of a mirror of the confusion of Swift devs like myself at the time, who were excited about Swift protocols and [&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":"2018-11-29T21:43:52Z","apple_news_api_id":"2b1f48ed-b012-49ab-8fd1-eae2fe328153","apple_news_api_modified_at":"2018-11-29T21:43:54Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/AKx9I7bASSauP0eri_jKBUw","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-23540","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\/23540","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=23540"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/23540\/revisions"}],"predecessor-version":[{"id":23541,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/23540\/revisions\/23541"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=23540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=23540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=23540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}