{"id":14622,"date":"2016-05-24T11:07:05","date_gmt":"2016-05-24T15:07:05","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=14622"},"modified":"2016-05-24T11:07:05","modified_gmt":"2016-05-24T15:07:05","slug":"swift-tuples-arent-equatable","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2016\/05\/24\/swift-tuples-arent-equatable\/","title":{"rendered":"Swift Tuples Aren&rsquo;t Equatable"},"content":{"rendered":"<p><a href=\"http:\/\/ericasadun.com\/2016\/05\/23\/swifty-quicky-checking-for-a-tuple-in-an-array\/\">Erica Sadun<\/a> notes that Swift will let you create an array of tuples:<\/p>\n<pre>let a: [(Int, Int)] = [(1, 2), (3, 4)]<\/pre>\n<p>But you can&rsquo;t use the <code>contains()<\/code> method:<\/p>\n<pre>a.contains((3, 4)) \/\/ no!<\/pre>\n<p>Its definition looks like:<\/p>\n<pre>extension SequenceType where Generator.Element : Equatable {\n    \/\/\/ Returns `true` iff `element` is in `self`.\n    @warn_unused_result\n    public func contains(element: Self.Generator.Element) -&gt; Bool\n}<\/pre>\n<p>The problem is that it requires the element to conform to the <code>Equatable<\/code> protocol. <code>Equatable<\/code> only includes one method:<\/p>\n<pre>func ==(lhs: Self, rhs: Self) -&gt; Bool<\/pre>\n<p>And the tuple <em>does<\/em> implement <code>==<\/code>. But the compiler can&rsquo;t use that fact because Swift doesn&rsquo;t allow tuples to conform to protocols. This is the sort of thing that <a href=\"http:\/\/mjtsai.com\/blog\/2016\/05\/21\/dynamic-swift-2\/\">annoys people<\/a> about static languages. The workaround is to use a different version of <code>contains()<\/code>:<\/p>\n<pre>extension SequenceType {\n    \/\/\/ Returns `true` iff an element in `self` satisfies `predicate`.\n    @warn_unused_result\n    public func contains(@noescape predicate: (Self.Generator.Element) throws -&gt; Bool) rethrows -&gt; Bool\n}<\/pre>\n<p>to call <code>==<\/code> directly via a closure:<\/p>\n<pre>a.contains({ $0 == (3, 4) })<\/pre>","protected":false},"excerpt":{"rendered":"<p>Erica Sadun notes that Swift will let you create an array of tuples: let a: [(Int, Int)] = [(1, 2), (3, 4)] But you can&rsquo;t use the contains() method: a.contains((3, 4)) \/\/ no! Its definition looks like: extension SequenceType where Generator.Element : Equatable { \/\/\/ Returns `true` iff `element` is in `self`. @warn_unused_result public func [&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":[46,71,901],"class_list":["post-14622","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\/14622","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=14622"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/14622\/revisions"}],"predecessor-version":[{"id":14623,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/14622\/revisions\/14623"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=14622"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=14622"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=14622"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}