{"id":20746,"date":"2018-02-28T14:55:29","date_gmt":"2018-02-28T19:55:29","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=20746"},"modified":"2021-06-01T16:27:15","modified_gmt":"2021-06-01T20:27:15","slug":"swift-protocols-wishlist","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2018\/02\/28\/swift-protocols-wishlist\/","title":{"rendered":"Swift Protocols Wish List"},"content":{"rendered":"<p><a href=\"https:\/\/davedelong.com\/blog\/2018\/02\/08\/swift-protocols-wishlist\/\">Dave DeLong<\/a>:<\/p>\n<blockquote cite=\"https:\/\/davedelong.com\/blog\/2018\/02\/08\/swift-protocols-wishlist\/\">\n<p>If you&rsquo;re adopting a protocol (especially one from the standard library), the tools don&rsquo;t provide any help whatsoever in knowing what you actually have to implement.<\/p>\n<p>[&#8230;]<\/p>\n<p>Basically any time you find yourself using a PAT, you know that you&rsquo;re either going to have to create a type eraser, or you&rsquo;re going to have to make things generic that have no business being generic.<\/p>\n<p>[&#8230;]<\/p>\n<p>I wish the compiler generated type erasers for me.<\/p>\n<p>[&#8230;]<\/p>\n<p>I wish I could provide default implementations of protocol methods <em>that assign to self<\/em>.<\/p>\n<p>[&#8230;]<\/p>\n<p>I wish I could extend a protocol to conform to another protocol.<\/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\/2016\/06\/01\/swift-type-erasure\/\">Swift Type Erasure<\/a>, <a href=\"https:\/\/mjtsai.com\/blog\/2017\/12\/20\/patterns-for-working-with-associated-types\/\">Patterns for Working With Associated Types<\/a>.<\/p>\n\n<p>Update (2018-03-05): See also: <a href=\"https:\/\/spec.fm\/podcasts\/swift-unwrapped\/117707\">Swift Unwrapped<\/a>.<\/p>\n\n<p>Update (2018-07-27): <a href=\"https:\/\/forums.swift.org\/t\/questions-about-the-future-of-protocols-with-associated-types\/14784\">Dave DeLong<\/a> (<a href=\"https:\/\/twitter.com\/davedelong\/status\/1022583599642501121\">tweet<\/a>):<\/p>\n<blockquote cite=\"https:\/\/forums.swift.org\/t\/questions-about-the-future-of-protocols-with-associated-types\/14784\"><p>What I <em>really<\/em> want to do is this:<\/p>\n<pre>protocol Predicate&lt;Element&gt; {\n    func contains(_ other: Element) -&gt; Bool\n    func union(_ other: Predicate&lt;Element&gt;) -&gt; Predicate&lt;Element&gt;\n}<\/pre>\n<p>Of course, the compiler doesn't let me do this.<\/p>\n<p>I want to express the idea that <code>union()<\/code> can take any <code>Predicate<\/code>-conforming value with the same element type, and you're going to get back <em>some sort<\/em> of <code>Predicate<\/code>-conforming value that contains elements of that type.<\/p>\n<p>Then in specific situations where I can provide a much more efficient implementation (like the <code>IndexSet<\/code> + <code>IndexSet<\/code> = <code>IndexSet<\/code> scenario), I want to provide those explicit overrides for that method, and otherwise default back to the implementations I provide in my protocol extension (which <em>happen<\/em> to return an <code>OrPredicate&lt;Int&gt;<\/code>)<\/p>\n<\/blockquote>\n<p><a href=\"https:\/\/forums.swift.org\/t\/questions-about-the-future-of-protocols-with-associated-types\/14784\/5?u=mjtsai\">Brent Royal-Gordon<\/a>:<\/p>\n<blockquote cite=\"https:\/\/forums.swift.org\/t\/questions-about-the-future-of-protocols-with-associated-types\/14784\/5?u=mjtsai\"><p>What I think you actually want here <em>is<\/em> the generalized existential feature. &ldquo;Generalized existential&rdquo; is a ten-dollar term for a ten-cent idea: &ldquo;Swift should have built-in type-erasing wrappers for PATs, much like it has them for ordinary protocols.&rdquo; Their syntax might be slightly different&mdash;they&rsquo;d probably use a <code>where<\/code> clause instead of a generic parameter list&mdash;but they would do the thing you want to do here.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Dave DeLong: If you&rsquo;re adopting a protocol (especially one from the standard library), the tools don&rsquo;t provide any help whatsoever in knowing what you actually have to implement. [&#8230;] Basically any time you find yourself using a PAT, you know that you&rsquo;re either going to have to create a type eraser, or you&rsquo;re going to [&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-07-27T18:38:04Z","apple_news_api_id":"089038d3-c958-45f5-937c-38252e0b4549","apple_news_api_modified_at":"2021-06-01T20:27:19Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAABA==","apple_news_api_share_url":"https:\/\/apple.news\/ACJA408lYRfWTfDglLgtFSQ","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-20746","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\/20746","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=20746"}],"version-history":[{"count":8,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/20746\/revisions"}],"predecessor-version":[{"id":32663,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/20746\/revisions\/32663"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=20746"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=20746"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=20746"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}