{"id":20096,"date":"2018-01-08T15:36:18","date_gmt":"2018-01-08T20:36:18","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=20096"},"modified":"2019-01-18T13:29:19","modified_gmt":"2019-01-18T18:29:19","slug":"conditional-conformance-in-the-standard-library","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2018\/01\/08\/conditional-conformance-in-the-standard-library\/","title":{"rendered":"Conditional Conformance in the Standard Library"},"content":{"rendered":"<p><a href=\"https:\/\/swift.org\/blog\/conditional-conformance\/\">Ben Cohen<\/a>:<\/p>\n<blockquote cite=\"https:\/\/swift.org\/blog\/conditional-conformance\/\"><p>The most noticeable benefit of conditional conformance is the ability for types\nthat store other types, like <code>Array<\/code> or <code>Optional<\/code>, to conform to the\n<code>Equatable<\/code> protocol. This is the protocol that guarantees you can use <code>==<\/code>\nbetween two instances of a type.<\/p><p>[&#8230;]<\/p><p>Having equatable elements gives collections other helper functions for tasks like searching[&#8230;]<\/p><p>[&#8230;]<\/p><p>This approach also works for <code>Codable<\/code>. If you try and encode an array\nof non-codable types, you&rsquo;ll now get a compile-time error instead of the runtime\ntrap you used to get.<\/p><p>[&#8230;]<\/p><p>If the collection we&rsquo;re splitting is bidirectional, we ought to be able to make\nour splitting wrapper bidirectional too. In Swift 4.0, the way to do this was\npretty clunky. You had to add a whole new type,\n<code>LazySplitBidirectionalCollection<\/code>, which required <code>Base:\nBidirectionalCollection<\/code> and implemented <code>BidirectionalCollection<\/code>. Then, you\noverloaded the <code>split<\/code> method to return it <code>where Base:\nBidirectionalCollection<\/code>.<\/p><p>Now, with conditional conformance, we have a much simpler solution: just make\n<code>LazySplitCollection<\/code> conform to <code>BidirectionalCollection<\/code> when its base does.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Ben Cohen: The most noticeable benefit of conditional conformance is the ability for types that store other types, like Array or Optional, to conform to the Equatable protocol. This is the protocol that guarantees you can use == between two instances of a type.[&#8230;]Having equatable elements gives collections other helper functions for tasks like searching[&#8230;][&#8230;]This [&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":"2019-01-18T15:29:40Z","apple_news_api_id":"3827ece3-350a-4fa1-9517-85409bd61e55","apple_news_api_modified_at":"2019-01-18T18:29:23Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAA==","apple_news_api_share_url":"https:\/\/apple.news\/AOCfs4zUKT6GVF4VAm9YeVQ","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,1774,901],"class_list":["post-20096","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-programming","tag-swift-codable","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/20096","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=20096"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/20096\/revisions"}],"predecessor-version":[{"id":20097,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/20096\/revisions\/20097"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=20096"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=20096"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=20096"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}