<div dir="ltr"><div>I was recently writing a simple state machine where the machine state has simple deterministic rules for the state transitions given the current state and the input. I felt both the state and the input types lent themselves well to sealed classes and records but while implementing the logic I had to resort to if-else statements. At that point, I felt if I were able to switch and pattern-match on both the input types together as a tuple, my logic would have been much clearer while also concise.<br></div><div><br></div><div>Simplified sample code that I wish to be able to write (although the point is not the exact syntax):</div><div style="background-color:rgb(30,31,34);color:rgb(188,190,196);font-family:"Consolas",monospace;font-size:9.8pt;white-space:pre"><span style="color:rgb(207,142,109)">sealed interface </span>State {}<br><span style="color:rgb(207,142,109)">record </span>Start() <span style="color:rgb(207,142,109)">implements </span>State {}<br><span style="color:rgb(207,142,109)">record </span>First() <span style="color:rgb(207,142,109)">implements </span>State {}<br><span style="color:rgb(207,142,109)">record </span>Second() <span style="color:rgb(207,142,109)">implements </span>State {}<br><span style="color:rgb(207,142,109)">record </span>End() <span style="color:rgb(207,142,109)">implements </span>State {}<br><br><span style="color:rgb(207,142,109)">sealed interface </span>Move {}<br><span style="color:rgb(207,142,109)">record </span>Jump(<span style="color:rgb(207,142,109)">int </span>steps) <span style="color:rgb(207,142,109)">implements </span>Move {<br>    <span style="color:rgb(86,168,245)">Jump </span>{<br>        <span style="color:rgb(207,142,109)">if </span>(steps < <span style="color:rgb(42,172,184)">0 </span>|| steps > <span style="color:rgb(42,172,184)">2</span>)<br>            <span style="color:rgb(207,142,109)">throw new </span>IllegalArgumentException();<br>    }<br>}<br><span style="color:rgb(207,142,109)">record </span>None() <span style="color:rgb(207,142,109)">implements </span>Move {}<br><br>State <span style="color:rgb(86,168,245)">transition</span>(State initial, Move move) {<br>    <span style="color:rgb(207,142,109)">return switch</span>(initial, move) {<br>        <span style="color:rgb(207,142,109)">case </span>(Start, Jump(<span style="color:rgb(42,172,184)">1</span>)) -> First;<br>        <span style="color:rgb(207,142,109)">case </span>(Start, Jump(<span style="color:rgb(42,172,184)">2</span>)) | (First, Jump(<span style="color:rgb(42,172,184)">1</span>)) -> Second;<br>        <span style="color:rgb(207,142,109)">case </span>(First, Jump(<span style="color:rgb(42,172,184)">2</span>)) | (Second, Jump(_)) -> End;<br>        <span style="color:rgb(207,142,109)">case </span>(End, _) -> End;<br>        <span style="color:rgb(207,142,109)">case </span>(None, _) -> initial;<br>    }<br>}</div><div><br></div><div>I did not see in the amber docs about being able to use multiple patterns in a switch expression in any of the future plans. It does not even have to be a switch really but something to allow this type of expressivity. This feels very much in line with the Data Oriented programming article published a while ago.</div><div><br></div><div>Even if something like this were possible, I am guessing a default clause would be needed in the switch because the compiler cannot infer that Jump(1) and Jump(2) are the only valid jumps. Being able to help the compiler  by expressing such constraints at the language level would be even more amazing but that is a whole other topic.<br></div><div><br></div><div>Would something like this be possible one day?<br></div><div><br></div><div><div><div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div>Regards<br></div><div>Swaranga</div></div></div></div></div></div></div>