{"id":25299,"date":"2019-05-17T16:38:56","date_gmt":"2019-05-17T20:38:56","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=25299"},"modified":"2019-05-17T16:38:56","modified_gmt":"2019-05-17T20:38:56","slug":"dynamic-equality-checking-and-equatable","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2019\/05\/17\/dynamic-equality-checking-and-equatable\/","title":{"rendered":"Dynamic Equality Checking and Equatable"},"content":{"rendered":"<p><a href=\"https:\/\/forums.swift.org\/t\/dynamic-equality-checking-and-equatable\/24556\">Tanner Bennett<\/a> (<a href=\"https:\/\/twitter.com\/NSExceptional\/status\/1128389751449554944\">tweet<\/a>):<\/p>\n<blockquote cite=\"https:\/\/forums.swift.org\/t\/dynamic-equality-checking-and-equatable\/24556\">\n<p>Note that the arguments for <code>==<\/code> are defined as <code>Self<\/code> rather than <code>Equatable<\/code>. This has some implications and benefits[&#8230;]<\/p>\n<p>[&#8230;]<\/p>\n<p>The use of <code>Self<\/code> as in <code>Equatable<\/code>&rsquo;s definition restricts how <code>Equatable<\/code> can be used. You cannot declare collections or variables as a protocol type, like you can in Objective-C.<\/p>\n<p>[&#8230;]<\/p>\n<p>Many APIs vend <code>Any<\/code>, such as <code>JSONSerialization<\/code>&rsquo;s <code>.jsonObject(_:_:)<\/code> methods. For testing purposes, you may want to compare the output of these methods to one another. You&rsquo;ll quickly find you can&rsquo;t[&#8230;]<\/p>\n<\/blockquote>\n\n<p>See also: <a href=\"https:\/\/forums.swift.org\/t\/adding-a-polymorphic-equatable\/12034\">Adding a polymorphic Equatable?<\/a>, <a href=\"https:\/\/developer.apple.com\/videos\/play\/wwdc2015\/408\/?time=2231\">Protocol-Oriented Programming in Swift<\/a>.<\/p>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2015\/09\/01\/swift-protocols\/\">Swift Protocols<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2015\/07\/22\/every-swift-value-type-should-be-equatable\/\">Every Swift Value Type Should Be Equatable<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Tanner Bennett (tweet): Note that the arguments for == are defined as Self rather than Equatable. This has some implications and benefits[&#8230;] [&#8230;] The use of Self as in Equatable&rsquo;s definition restricts how Equatable can be used. You cannot declare collections or variables as a protocol type, like you can in Objective-C. [&#8230;] Many APIs [&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-05-17T20:38:58Z","apple_news_api_id":"04fb3b4b-1e27-44ca-b064-08de1b4ee381","apple_news_api_modified_at":"2019-05-17T20:39:00Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/ABPs7Sx4nRMqwZAjeG07jgQ","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":[507,46,71,901],"class_list":["post-25299","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-json","tag-languagedesign","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/25299","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=25299"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/25299\/revisions"}],"predecessor-version":[{"id":25300,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/25299\/revisions\/25300"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=25299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=25299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=25299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}