{"id":40223,"date":"2023-08-01T13:27:47","date_gmt":"2023-08-01T17:27:47","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=40223"},"modified":"2023-08-01T14:38:44","modified_gmt":"2023-08-01T18:38:44","slug":"swift-enum-pattern-matching-with-extra-conditions","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2023\/08\/01\/swift-enum-pattern-matching-with-extra-conditions\/","title":{"rendered":"Swift Enum Pattern Matching With Extra Conditions"},"content":{"rendered":"<p><a href=\"https:\/\/nilcoalescing.com\/blog\/SwiftEnumPatternMatchingWithExtraConditions\/\">Natalia Panferova<\/a>:<\/p>\n<blockquote cite=\"https:\/\/nilcoalescing.com\/blog\/SwiftEnumPatternMatchingWithExtraConditions\/\">\n<p>In this post we are going to explore how we can provide more precise conditions for pattern matching when working with enums in Swift. The most common use case is using <code>switch<\/code> statements with the <code>where<\/code> clause to get more control over case conditions. But we'll also look into using the <code>where<\/code> clause in <code>for-in<\/code> loops to avoid unnecessary extra iterations. And finally, we'll see how to add extra conditions in <code>while<\/code> loops and <code>if-case<\/code> statements when the <code>where<\/code> clause is not available.<\/p>\n<\/blockquote>\n\n<p>I&rsquo;ve always found Swift&rsquo;s <code>if case<\/code> syntax confusing, because you have to put the constant first, and then you use <code>=<\/code>, which looks like an assignment, instead of <code>==<\/code>. It makes more sense if you think about the variant Panferova shows, where there are associated values. Then it becomes <code>if case let<\/code>, and the order and operator are what you&rsquo;d expect for a <code>let<\/code>. I generally write <code>if anEnum == .aCase<\/code> if I don&rsquo;t need to bind any variables, but my recollection is that this either wasn&rsquo;t supported or had issues in earlier versions of Swift.<\/p>\n\n<p><a href=\"https:\/\/christiantietze.de\/posts\/2023\/06\/swift-pattern-matching-extra-conditions-value-of-syntax-summaries\/\">Christian Tietze<\/a>:<\/p>\n<blockquote cite=\"https:\/\/christiantietze.de\/posts\/2023\/06\/swift-pattern-matching-extra-conditions-value-of-syntax-summaries\/\">\n<p>I believe there&rsquo;s tremendous value in summaries like these to learn the Swift programming language and its syntax: these short summaries show a slice of different aspects of the language in close proximity.<\/p>\n<p>[&#8230;]<\/p>\n<p>It&rsquo;s zooming in on where-clauses, and so the reader gets to know a different &ldquo;view&rdquo; into the syntax as a whole that is different from the book&rsquo;s presentation.<\/p>\n<\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2023\/03\/13\/pattern-matching-on-swift-error-codes\/\">Pattern Matching on Swift Error Codes<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2018\/11\/01\/swifts-if-case-let-syntax\/\">Swift&rsquo;s &ldquo;if case let&rdquo; Syntax<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2016\/06\/23\/restructuring-swift-condition-clauses\/\">Restructuring Swift Condition Clauses<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2015\/04\/02\/custom-swift-switch-matchers\/\">Custom Swift Switch Matchers<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Natalia Panferova: In this post we are going to explore how we can provide more precise conditions for pattern matching when working with enums in Swift. The most common use case is using switch statements with the where clause to get more control over case conditions. But we'll also look into using the where clause [&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":"2023-08-01T17:27:49Z","apple_news_api_id":"984b2168-b8fa-4f32-84f0-9d441b7798c2","apple_news_api_modified_at":"2023-08-01T18:38:47Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAg==","apple_news_api_share_url":"https:\/\/apple.news\/AmEshaLj6TzKE8J1EG3eYwg","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-40223","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\/40223","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=40223"}],"version-history":[{"count":4,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/40223\/revisions"}],"predecessor-version":[{"id":40232,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/40223\/revisions\/40232"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=40223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=40223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=40223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}