{"id":19910,"date":"2017-12-20T11:17:33","date_gmt":"2017-12-20T16:17:33","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=19910"},"modified":"2017-12-20T11:17:33","modified_gmt":"2017-12-20T16:17:33","slug":"patterns-for-working-with-associated-types","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2017\/12\/20\/patterns-for-working-with-associated-types\/","title":{"rendered":"Patterns for Working With Associated Types"},"content":{"rendered":"<p><a href=\"https:\/\/appventure.me\/2017\/12\/10\/patterns-for-working-with-associated-types\/\">Benedikt Terhechte<\/a>:<\/p>\n<blockquote cite=\"https:\/\/appventure.me\/2017\/12\/10\/patterns-for-working-with-associated-types\/\">\n<p>The first solution for the archetypical problem is also a really simple one. Instead of enforcing <code>Equatable<\/code> on your custom <code>protocol<\/code>, you can simply require your full fledged, final, types to conform to the <code>Equatable<\/code> protocol instead of your custom protocol.<\/p>\n<p>[&#8230;]<\/p>\n<p>As you can see in the example above, using <code>Self<\/code> as a method parameter or using <code>Self<\/code> as a property type automatically introduces an <code>associated type<\/code> (like we saw with <code>Equatable<\/code>, earlier).<\/p>\n<p>The most helpful note here is that once you use a <code>method<\/code> instead of a <code>property<\/code> in order to return something of type <code>Self<\/code> you will not opt in to an <code>associated type<\/code>[&#8230;]<\/p>\n<p>[&#8230;]<\/p>\n<p>The idea here is that you define two protocols that share common methods. Only one of those protocols contains <code>associated types<\/code>, the other does not. Your types conform to both protocols. This means that you can use the <b>normal<\/b> protocol as a type for all situations. If you, then, need to use the parts of the type that only affect the <code>associated type<\/code>, you can do so by means of a runtime cast.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Benedikt Terhechte: The first solution for the archetypical problem is also a really simple one. Instead of enforcing Equatable on your custom protocol, you can simply require your full fledged, final, types to conform to the Equatable protocol instead of your custom protocol. [&#8230;] As you can see in the example above, using Self as [&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":[],"tags":[71,901],"class_list":["post-19910","post","type-post","status-publish","format-standard","hentry","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/19910","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=19910"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/19910\/revisions"}],"predecessor-version":[{"id":19911,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/19910\/revisions\/19911"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=19910"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=19910"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=19910"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}