{"id":12050,"date":"2015-08-21T10:16:03","date_gmt":"2015-08-21T14:16:03","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=12050"},"modified":"2015-08-21T10:17:17","modified_gmt":"2015-08-21T14:17:17","slug":"swift-pattern-matching-in-detail","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/08\/21\/swift-pattern-matching-in-detail\/","title":{"rendered":"Swift Pattern Matching in Detail"},"content":{"rendered":"<p><a href=\"http:\/\/appventure.me\/2015\/08\/20\/swift-pattern-matching-in-detail\/\">Benedikt Terhechte<\/a> (via <a href=\"https:\/\/twitter.com\/clattner_llvm\/status\/634386605403336704\">Chris Lattner<\/a>):<\/p>\n<blockquote cite=\"http:\/\/appventure.me\/2015\/08\/20\/swift-pattern-matching-in-detail\/\">\n<p>The main feature of <code>switch<\/code> is of course pattern matching, the ability to destructure values and match different switch cases based on correct match of the values to the cases.<\/p>\n<p>[&#8230;]<\/p>\n<p>So, how do you best implement this new restriction? You could just have an <code>if<\/code> \/ <code>else<\/code> switch for buy and for sell, but that would lead to nested code which quickly lacks clarity - and who knows maybe these Wall Street guys come up with further complications. So you define it instead as additional requirements on the pattern matches[&#8230;]<\/p>\n<p>[&#8230;]<\/p>\n<p>By default, and unlike C\/C++\/Objective-C, <code>switch<\/code> <code>cases<\/code> do not fall through into the next case which is why in Swift, you don&rsquo;t need to write <code>break<\/code> for every case. You can opt into traditional fallthrough behaviour with the <code>fallthrough<\/code> keyword.<\/p>\n<p>[&#8230;]<\/p>\n<p>The <code>case<\/code> keyword can be used in for loops just like in <code>switch<\/code> cases.<\/p>\n<\/blockquote>\n<p><code>case<\/code> can also be used with <code>guard<\/code>, although only wildcard and identifier patterns work with <code>for<\/code> and <code>guard<\/code>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Benedikt Terhechte (via Chris Lattner): The main feature of switch is of course pattern matching, the ability to destructure values and match different switch cases based on correct match of the values to the cases. [&#8230;] So, how do you best implement this new restriction? You could just have an if \/ else switch for [&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-12050","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\/12050","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=12050"}],"version-history":[{"count":3,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/12050\/revisions"}],"predecessor-version":[{"id":12055,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/12050\/revisions\/12055"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=12050"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=12050"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=12050"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}