<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">
For those following along: Olexandr is appealing to a feature of C# where `< n` is a pattern which is applicable to numeric types, and matches when the inequality holds.  If we had such patterns, switch wouldn’t need to change.  (Such patterns are not currently
 on the roadmap, for a number of reasons but mostly because there are so many higher-value features ahead of it in the queue.). 
<div class="">
<div><br class="">
<blockquote type="cite" class="">
<div class="">On Oct 1, 2024, at 9:39 AM, Olexandr Rotan <<a href="mailto:rotanolexandr842@gmail.com" class="">rotanolexandr842@gmail.com</a>> wrote:</div>
<br class="Apple-interchange-newline">
<div class="">
<div dir="ltr" class="">I would say that the moment there is something except i in switch you are violating SRP on statement level. If I were to advocate for alike feature, I would much rather support something like<br class="">
<br class="">
<div class="">
<div class=""> var res = switch (i) {</div>
<div class="">    case < 5 -> 5;</div>
<div class="">    case > 10 -> 10;</div>
<div class="">    case null -> 0<br class="">
</div>
<div class="">    default -> throw new IllegalStateException("Unexpected value: " + new Object());</div>
</div>
<div class="">};</div>
<div class=""><br class="">
</div>
<div class="">(something like this is present in .NET). Adding case ... j.length() < 10 -> 20; seems like it steps outside of switch scope, which is pattern matching on switch argument, to me.<br class="">
<br class="">
Regrads</div>
</div>
<br class="">
<div class="gmail_quote">
<div dir="ltr" class="gmail_attr">On Tue, Oct 1, 2024 at 4:20 PM Brian Goetz <<a href="mailto:brian.goetz@oracle.com" class="">brian.goetz@oracle.com</a>> wrote:<br class="">
</div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div style="overflow-wrap: break-word;" class="">Yes, this has been considered several times.  It is one of those things that seems clever at first, and it is not intrinsically flawed, but over time seems less attractive, for a number of reasons.  
<div class=""><br class="">
</div>
<div class="">To start with, let’s recognize that `switch` doesn’t really need to exist; it doesn’t express anything that we can’t express with `if` and other control flows.  So why do we have it?  Because it is _more constrained_ than arbitrary if-else-break
 logic, and therefore we can write simpler programs with it.  If we give switch more kinds of jobs to do, we risk giving up that simplicity benefit.  </div>
<div class=""><br class="">
</div>
<div class="">The essence of switch is to ask “is this thing one of these, if so, do the appropriate thing.”  It started out very limited (limited in selector types and the kinds of cases), and we generalized it to more types and more powerful cases (through
 patterns and guards), while at the same time, retaining that essence of “pick one based on what this target is.”</div>
<div class=""><br class="">
</div>
<div class="">So the answer to your question is: yes, it’s been considered; yes, it’s not totally dumb (many proposed language features don’t meet this bar); but no, it does not seem the best thing we could be working on now.</div>
<div class=""><br class="">
</div>
<div class="">
<div class="">
<div class=""><br class="">
<blockquote type="cite" class="">
<div class="">On Oct 1, 2024, at 9:09 AM, mirage <<a href="mailto:newblood17@gmail.com" target="_blank" class="">newblood17@gmail.com</a>> wrote:</div>
<br class="">
<div class="">
<div dir="ltr" class="">
<div dir="ltr" class="">
<div dir="ltr" class="">
<div dir="ltr" class="">
<div dir="ltr" class="">Hello. 
<div class=""><br class="">
</div>
<div class="">In the recent years switch has been enhanced and there are some discussions about enhancing it even further with try - catch evaluation style inside the switch itself.</div>
<div class=""><br class="">
</div>
<div class="">i was wondering if an additional enhancement could be to be able to use an empty parameter switch so we can use switch as a general-purpose replacement of if-else chains. Switch has many advantages over if-else chains, being these the most relevant.</div>
<div class=""><br class="">
</div>
<div class="">- The code it's cleaner and more readable, even with nested switch expressions.</div>
<div class="">- Switch can be an expression, so it can return an assign a value to a variable.</div>
<div class=""><br class="">
</div>
<div class="">nowadays we can use something like this.</div>
<div class=""><br class="">
</div>
<div class="">
<div class="">var i = someIntMethodParameter; </div>
<div class="">var j = someStringMethodParameter</div>
<div class="">var res = switch (new Object()){</div>
<div class="">    case Object _ when i < 5 -> 5;</div>
<div class="">    case Object _ when i > 10 -> 10;</div>
<div class="">    case Object _ when j.length() < 10 -> 20;</div>
<div class="">    case null -> 0;</div>
<div class="">    default -> throw new IllegalStateException("Unexpected value: " + new Object());</div>
</div>
<div class="">};</div>
<div class=""><br class="">
</div>
<div class="">as we can see here this is effectively a replacement for if-else chains that returns the first true case, but there are some problems with this syntax and semantics</div>
<div class=""><br class="">
</div>
<div class="">. I was wondering if it could be a good idea to </div>
<div class=""><br class="">
</div>
<div class="">
<pre style="max-width:100%;margin-bottom:1rem;border-radius:4px;padding:1rem 1.4rem;font-size:0.9em;overflow:auto;color:rgb(242,242,242)" class=""><b class="">
};</b></pre>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
<br class="">
</div>
</div>
</div>
</blockquote>
</div>
</div>
</blockquote>
</div>
<br class="">
</div>
</body>
</html>