{"id":365,"date":"2012-04-14T22:03:05","date_gmt":"2012-04-14T20:03:05","guid":{"rendered":"http:\/\/misto.ch\/?p=365"},"modified":"2012-04-14T22:03:05","modified_gmt":"2012-04-14T20:03:05","slug":"expand-case-class-bindings-in-pattern-matching","status":"publish","type":"post","link":"https:\/\/misto.ch\/de\/2012\/04\/14\/expand-case-class-bindings-in-pattern-matching\/","title":{"rendered":"Quickfix to Expand Case-Class Bindings in Pattern Matching"},"content":{"rendered":"<p>When writing Scala code that involves pattern matching I often work with (nested) case classes and sequences of case classes. I usually start with a simple binding like this<\/p>\n<pre class=\"brush: scala; title: ; notranslate\" title=\"\">\ncase class Person(firstName: String, lastName: String)\n\nList(Person(&quot;Mirko&quot;, &quot;Stocker&quot;)) match {\n  case person :: Nil =&gt;; ...\n}\n<\/pre>\n<p>and then when I need to access members of the matched class convert it to use the extractor:<\/p>\n<pre class=\"brush: scala; title: ; notranslate\" title=\"\">\nList(Person(&quot;Mirko&quot;, &quot;Stocker&quot;)) match {\n  case Person(firstName, lastName) :: Nil =&gt; ...\n}\n<\/pre>\n<p>What&#8217;s tedious about that step is that one needs to know how many arguments there are and name all the bindings. But not for much longer! I wrote a quick-fix for the Scala IDE that does this for you:<\/p>\n<p><a href=\"https:\/\/misto.ch\/wp-content\/uploads\/2012\/04\/expand_case_class_binding_1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-375 alignleft\" title=\"expand_case_class_binding_1\" src=\"https:\/\/misto.ch\/wp-content\/uploads\/2012\/04\/expand_case_class_binding_1.png\" alt=\"\" width=\"495\" height=\"123\" srcset=\"https:\/\/misto.ch\/wp-content\/uploads\/2012\/04\/expand_case_class_binding_1.png 495w, https:\/\/misto.ch\/wp-content\/uploads\/2012\/04\/expand_case_class_binding_1-300x75.png 300w\" sizes=\"auto, (max-width: 495px) 100vw, 495px\" \/><\/a><\/p>\n<p>Ctrl+1 brings up the available quick fixes:<\/p>\n<p><a href=\"https:\/\/misto.ch\/wp-content\/uploads\/2012\/04\/expand_case_class_binding_2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-376 alignleft\" title=\"expand_case_class_binding_2\" src=\"https:\/\/misto.ch\/wp-content\/uploads\/2012\/04\/expand_case_class_binding_2.png\" alt=\"\" width=\"512\" height=\"219\" srcset=\"https:\/\/misto.ch\/wp-content\/uploads\/2012\/04\/expand_case_class_binding_2.png 512w, https:\/\/misto.ch\/wp-content\/uploads\/2012\/04\/expand_case_class_binding_2-300x128.png 300w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/misto.ch\/wp-content\/uploads\/2012\/04\/expand_case_class_binding_3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-377 alignleft\" title=\"expand_case_class_binding_3\" src=\"https:\/\/misto.ch\/wp-content\/uploads\/2012\/04\/expand_case_class_binding_3.png\" alt=\"\" width=\"497\" height=\"104\" srcset=\"https:\/\/misto.ch\/wp-content\/uploads\/2012\/04\/expand_case_class_binding_3.png 497w, https:\/\/misto.ch\/wp-content\/uploads\/2012\/04\/expand_case_class_binding_3-300x63.png 300w\" sizes=\"auto, (max-width: 497px) 100vw, 497px\" \/><\/a><\/p>\n<p>And this is what happens when the binding you&#8217;re expanding is used in the pattern&#8217;s body:<\/p>\n<p><a href=\"https:\/\/misto.ch\/wp-content\/uploads\/2012\/04\/expand_case_class_binding_4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-378 alignleft\" title=\"expand_case_class_binding_4\" src=\"https:\/\/misto.ch\/wp-content\/uploads\/2012\/04\/expand_case_class_binding_4.png\" alt=\"\" width=\"495\" height=\"108\" srcset=\"https:\/\/misto.ch\/wp-content\/uploads\/2012\/04\/expand_case_class_binding_4.png 495w, https:\/\/misto.ch\/wp-content\/uploads\/2012\/04\/expand_case_class_binding_4-300x65.png 300w\" sizes=\"auto, (max-width: 495px) 100vw, 495px\" \/><\/a><\/p>\n<p>It also works if you use a typed pattern where the type is a case class:<\/p>\n<p><a href=\"https:\/\/misto.ch\/wp-content\/uploads\/2012\/04\/expand_case_class_binding_5.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-379\" title=\"expand_case_class_binding_5\" src=\"https:\/\/misto.ch\/wp-content\/uploads\/2012\/04\/expand_case_class_binding_5.png\" alt=\"\" width=\"511\" height=\"93\" \/><\/a><\/p>\n<p><a href=\"https:\/\/misto.ch\/wp-content\/uploads\/2012\/04\/expand_case_class_binding_6.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/misto.ch\/wp-content\/uploads\/2012\/04\/expand_case_class_binding_6.png\" alt=\"\" title=\"expand_case_class_binding_6\" width=\"504\" height=\"113\" class=\"alignleft size-full wp-image-387\" srcset=\"https:\/\/misto.ch\/wp-content\/uploads\/2012\/04\/expand_case_class_binding_6.png 504w, https:\/\/misto.ch\/wp-content\/uploads\/2012\/04\/expand_case_class_binding_6-300x67.png 300w\" sizes=\"auto, (max-width: 504px) 100vw, 504px\" \/><\/a><\/p>\n<p>This feature isn&#8217;t in the recently released <a href=\"http:\/\/scala-ide.org\/blog\/release-notes-2.1-Milestone-1.html\">Milestone 1<\/a>, but it should be part of the next one (or one of the upcoming <a href=\"http:\/\/scala-ide.org\/download\/nightly.html\">nightly builds<\/a>). Suggestions for a better wording &ndash; expand case class binding &ndash; are welcome \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When writing Scala code that involves pattern matching I often work with (nested) case classes and sequences of case classes. I usually start with a simple binding like this case class Person(firstName: String, lastName: String) List(Person(&quot;Mirko&quot;, &quot;Stocker&quot;)) match { case person :: Nil =&gt;; &#8230; } and then when I need to access members of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[37,38],"class_list":["post-365","post","type-post","status-publish","format-standard","hentry","category-scala","tag-refactoring","tag-scala"],"_links":{"self":[{"href":"https:\/\/misto.ch\/de\/wp-json\/wp\/v2\/posts\/365","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/misto.ch\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/misto.ch\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/misto.ch\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/misto.ch\/de\/wp-json\/wp\/v2\/comments?post=365"}],"version-history":[{"count":0,"href":"https:\/\/misto.ch\/de\/wp-json\/wp\/v2\/posts\/365\/revisions"}],"wp:attachment":[{"href":"https:\/\/misto.ch\/de\/wp-json\/wp\/v2\/media?parent=365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/misto.ch\/de\/wp-json\/wp\/v2\/categories?post=365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/misto.ch\/de\/wp-json\/wp\/v2\/tags?post=365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}