<div dir="ltr"><div class="gmail_default" style="font-family:monospace">Well, what Brian and RedIO are suggesting would be more like this.</div><div class="gmail_default" style="font-family:monospace"><br></div><div class="gmail_default" style="font-family:monospace">byte b = 123;<br></div><div class="gmail_default" style="font-family:monospace"><br></div><div class="gmail_default" style="font-family:monospace">System.out.println</div><div class="gmail_default" style="font-family:monospace">(</div><div class="gmail_default" style="font-family:monospace"> switch (b)</div><div class="gmail_default" style="font-family:monospace"> {</div><div class="gmail_default" style="font-family:monospace"><br></div><div class="gmail_default" style="font-family:monospace"> case -128..-1 -> "It's negative!";</div><div class="gmail_default" style="font-family:monospace"> case 0 -> "It's zero!";<br></div><div class="gmail_default" style="font-family:monospace">
<div class="gmail_default" style="font-family:monospace"> case 1..127 -> "It's positive!";</div><div class="gmail_default" style="font-family:monospace"></div>
</div><div class="gmail_default" style="font-family:monospace"><br></div><div class="gmail_default" style="font-family:monospace"> }</div><div class="gmail_default" style="font-family:monospace">)</div><div class="gmail_default" style="font-family:monospace">;<br></div><div class="gmail_default" style="font-family:monospace"><br></div><div class="gmail_default" style="font-family:monospace">The difference is, in this example, it would be EXHAUSTIVE, NO DEFAULT CLAUSE NECESSARY. Your example requires either a default or a type pattern to be exhaustive, as do basically all switches that use when clauses.</div><div class="gmail_default" style="font-family:monospace"><br></div><div class="gmail_default" style="font-family:monospace">That exhaustiveness is powerful because -- patterns compose. For example, if you have a complex set of number checks, this switch will tell you if you left a value out. That is the power of exhaustiveness. When you combine it with composition, you can take an arbitrarily complex domain, and the compiler will let you know if you missed a case.<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Mar 13, 2024 at 12:13 PM Josiah Noel <<a href="mailto:josiahnoel@gmail.com" target="_blank">josiahnoel@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">we sorta have this with switch guards<div><br></div><div><div style="background-color:rgb(28,31,34);padding:0px 0px 0px 2px"><div style="color:rgb(199,205,209);font-family:Consolas;white-space:pre-wrap"><p style="margin:0px"><span style="color:rgb(207,191,173)"> </span><span style="color:rgb(255,0,127)">switch</span><span style="color:rgb(207,191,173)"> </span><span style="color:rgb(249,250,244)">(</span><span style="color:rgb(207,191,173)">intyMcIntFace</span><span style="color:rgb(249,250,244)">)</span><span style="color:rgb(207,191,173)"> </span><span style="color:rgb(249,250,244)">{</span></p><p style="margin:0px"><span style="color:rgb(207,191,173)"> </span><span style="color:rgb(255,0,127)">case</span><span style="color:rgb(207,191,173)"> </span><span style="color:rgb(82,227,246)">Integer</span><span style="color:rgb(207,191,173)"> i </span><span style="color:rgb(197,104,28)">when</span><span style="color:rgb(207,191,173)"> i </span><span style="color:rgb(255,0,127)">></span><span style="color:rgb(207,191,173)"> </span><span style="color:rgb(196,140,255)">1</span><span style="color:rgb(207,191,173)"> </span><span style="color:rgb(255,0,127)">&&</span><span style="color:rgb(207,191,173)"> i </span><span style="color:rgb(255,0,127)"><</span><span style="color:rgb(207,191,173)"> </span><span style="color:rgb(196,140,255)">10</span><span style="color:rgb(207,191,173)"> </span><span style="color:rgb(255,0,127)">-></span><span style="color:rgb(207,191,173)"> </span><span style="color:rgb(249,250,244)">{}</span></p><p style="margin:0px"><span style="color:rgb(207,191,173)"> </span></p><p style="margin:0px"><span style="color:rgb(207,191,173)"> </span><span style="color:rgb(255,0,127)">case</span><span style="color:rgb(207,191,173)"> </span><span style="color:rgb(196,140,255)">42</span><span style="color:rgb(207,191,173)"> </span><span style="color:rgb(255,0,127)">-></span><span style="color:rgb(207,191,173)"> </span><span style="color:rgb(249,250,244)">{}</span></p><p style="margin:0px"><br></p><p style="margin:0px"><span style="color:rgb(207,191,173)"> </span><span style="color:rgb(255,0,127)">default</span><span style="color:rgb(207,191,173)"> </span><span style="color:rgb(255,0,127)">-></span><span style="color:rgb(207,191,173)"> </span><span style="color:rgb(255,0,127)">throw</span><span style="color:rgb(207,191,173)"> </span><span style="color:rgb(255,0,127)">new</span><span style="color:rgb(207,191,173)"> </span><span style="color:rgb(167,236,33)">IllegalArgumentException</span><span style="color:rgb(249,250,244)">(</span><span style="color:rgb(236,228,126)">"Unexpected value: "</span><span style="color:rgb(249,250,244)">)</span><span style="color:rgb(255,0,127)">;</span></p><p style="margin:0px"><span style="color:rgb(207,191,173)"> </span><span style="color:rgb(249,250,244)">}</span></p></div></div></div></div></div><input name="virtru-metadata" type="hidden" value="{"email-policy":{"disableCopyPaste":false,"disablePrint":false,"disableForwarding":false,"enableNoauth":false,"expandedWatermarking":false,"expires":false,"sms":false,"expirationNum":1,"expirationUnit":"days","isManaged":false,"persistentProtection":false},"attachments":{},"compose-id":"3","compose-window":{"secure":false}}"></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Mar 13, 2024 at 8:16 AM Red IO <<a href="mailto:redio.development@gmail.com" target="_blank">redio.development@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="auto">The switch statement saw a huge transformation over the past few releases. So I was quite surprised to realize that the current switch construct can't check the range of an value.<div dir="auto">Example case x is between y and z.</div><div dir="auto">I'm most likely not the first one to notice that. Is there any discussion about adding some sort of range pattern? Would fit in the notion of the switch checking patterns quite well. </div><div dir="auto"><br></div><div dir="auto">Great regards </div><div dir="auto">RedIODev </div></div>
</blockquote></div></div>
</blockquote></div>