{"id":10938,"date":"2015-04-02T08:59:12","date_gmt":"2015-04-02T12:59:12","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=10938"},"modified":"2019-03-13T15:21:28","modified_gmt":"2019-03-13T19:21:28","slug":"custom-swift-switch-matchers","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/04\/02\/custom-swift-switch-matchers\/","title":{"rendered":"Custom Swift Switch Matchers"},"content":{"rendered":"<p><a href=\"http:\/\/www.figure.ink\/blog\/2015\/2\/14\/custom-switch-matchers-in-swift\">Joshua Emmons<\/a>:<\/p>\n<blockquote cite=\"http:\/\/www.figure.ink\/blog\/2015\/2\/14\/custom-switch-matchers-in-swift\"><p>Wouldn&rsquo;t it be great if we could teach <code>switch<\/code> new matchers that knew how to deal with our custom objects? Believe it or not, there&rsquo;s an operator for that.<\/p><p>It&rsquo;s called the <em>pattern match operator<\/em>, sometimes known by its (less googleable) syntax: <code>~=<\/code>. And <code>switch<\/code> statements <a href=\"https:\/\/developer.apple.com\/library\/prerelease\/ios\/documentation\/Swift\/Conceptual\/Swift_Programming_Language\/Patterns.html#\/\/apple_ref\/doc\/uid\/TP40014097-CH36-ID426\">use it behind the scenes to compare values to their <code>case<\/code>s&rsquo; matching patterns<\/a>.<\/p><p>[&#8230;]<\/p><p>This is <a href=\"http:\/\/www.figure.ink\/blog\/2015\/1\/25\/polymorphism-with-return-types\">yet another example<\/a> of Swift employing polymorphism in an unexpected way to make the type system do work for us. Work that results in very clean, concise, and &mdash; dare I say it &mdash;&nbsp;<em>declarative<\/em> code.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Joshua Emmons: Wouldn&rsquo;t it be great if we could teach switch new matchers that knew how to deal with our custom objects? Believe it or not, there&rsquo;s an operator for that.It&rsquo;s called the pattern match operator, sometimes known by its (less googleable) syntax: ~=. And switch statements use it behind the scenes to compare values [&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-03-13T19:21:30Z","apple_news_api_id":"e14d8d97-99b6-4154-a41d-3042bae0d3f5","apple_news_api_modified_at":"2019-03-13T19:21:31Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/A4U2Nl5m2QVSkHTBCuuDT9Q","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-10938","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\/10938","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=10938"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10938\/revisions"}],"predecessor-version":[{"id":24593,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10938\/revisions\/24593"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=10938"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=10938"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=10938"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}