<div dir="ltr"><div dir="ltr"><div dir="ltr">Hello. <div><br></div><div>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><br></div><div>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><br></div><div>- The code it's cleaner and more readable, even with nested switch expressions.</div><div>- Switch can be an expression, so it can return an assign a value to a variable.</div><div><br></div><div>nowadays we can use something like this.</div><div><br></div><div><div>var i = someIntMethodParameter; </div><div>var j = someStringMethodParameter</div><div>var res = switch (new Object()){</div><div>    case Object _ when i < 5 -> 5;</div><div>    case Object _ when i > 10 -> 10;</div><div>    case Object _ when j.length() < 10 -> 20;</div><div>    case null -> 0;</div><div>    default -> throw new IllegalStateException("Unexpected value: " + new Object());</div></div><div>};</div><div><br></div><div>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><br></div><div>- we have to declare an object (or whatever other parameter) in the switch even if we are not using it.</div><div>- we have to use some pattern matching syntax, even if we are not using it. </div><div>. I was wondering if it could be a good idea to allow empty parameters switch. i could look something like this.</div><div><br></div><div><div><div>var res = switch (){</div><div>    case when i < 5 -> 5;</div><div>    case  when i > 10 -> 10;</div><div>    case when j.length() < 10 -> 20;</div><div>    default -> -1;</div></div><div>};</div></div><div><br></div><div>This way we could replace long if-else chains of validations with a better alternative, turning switch into a general-purpose control construct</div><div><br></div></div></div></div>