{"id":20864,"date":"2018-03-12T15:58:27","date_gmt":"2018-03-12T19:58:27","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=20864"},"modified":"2018-03-12T15:58:27","modified_gmt":"2018-03-12T19:58:27","slug":"better-strategies-through-types","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2018\/03\/12\/better-strategies-through-types\/","title":{"rendered":"Better Strategies Through Types"},"content":{"rendered":"<p><a href=\"http:\/\/www.figure.ink\/blog\/2018\/3\/11\/better-strategies-through-types\">Joshua Emmons<\/a>:<\/p>\n<blockquote cite=\"http:\/\/www.figure.ink\/blog\/2018\/3\/11\/better-strategies-through-types\">\n<p>We have to limit our delegate to <code>class<\/code> implementations because delegates are assumed to hold mutable state.<\/p>\n<p>[&#8230;]<\/p>\n<p>So rather than holding our strategy&rsquo;s implementation in instance methods that need to be instantiated, we&rsquo;re going to move it all up into <a href=\"https:\/\/developer.apple.com\/library\/content\/documentation\/Swift\/Conceptual\/Swift_Programming_Language\/Methods.html#\/\/apple_ref\/doc\/uid\/TP40014097-CH15-ID241\">type methods on the type<\/a>.<\/p>\n<p>[&#8230;]<\/p>\n<p>If all this <code>.Type<\/code> and <code>.self<\/code> stuff feels a little awkward, it&rsquo;s probably because Swift already supports this kind of thing as a language feature. It has a specific syntax just for passing around types that are used to specialize implementations. We know it as &ldquo;generics&rdquo;.<\/p>\n<\/blockquote>\n<p>However, a key difference is that, unlike delegates, the type cannot change at runtime.<\/p>","protected":false},"excerpt":{"rendered":"<p>Joshua Emmons: We have to limit our delegate to class implementations because delegates are assumed to hold mutable state. [&#8230;] So rather than holding our strategy&rsquo;s implementation in instance methods that need to be instantiated, we&rsquo;re going to move it all up into type methods on the type. [&#8230;] If all this .Type and .self [&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":[69,27,31,1472,30,1529,71,901],"class_list":["post-20864","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-craft","tag-ios","tag-ios-11","tag-mac","tag-macos-10-13","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/20864","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=20864"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/20864\/revisions"}],"predecessor-version":[{"id":20865,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/20864\/revisions\/20865"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=20864"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=20864"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=20864"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}