<div style="font-family: Arial, sans-serif; font-size: 14px;">Given that JEP 530 is a fourth preview, this feedback may be too late: I do not think switch patterns should do implicit type conversion without a special keyword. As while I think being able to test whether a float represents a valid int is very useful, I do not think instanceof should be misused to represent this concept of lossless implicit conversion. I instead recommend the addition of a "satisfies" keyword, like so:<br><br><blockquote style="border-left: 3px solid rgb(200, 200, 200); border-top-color: rgb(200, 200, 200); border-right-color: rgb(200, 200, 200); border-bottom-color: rgb(200, 200, 200); padding-left: 10px; color: rgb(102, 102, 102);"><div>if (0.5f satisfies int example) // false<br></div><div>if (1f satisfies int example) // true</div></blockquote><div><br></div><div>This keyword would be used in lieu of "instanceof" and "case", like so:<br><br><blockquote style="border-left: 3px solid rgb(200, 200, 200); border-top-color: rgb(200, 200, 200); border-right-color: rgb(200, 200, 200); border-bottom-color: rgb(200, 200, 200); padding-left: 10px; color: rgb(102, 102, 102);"><div>int result = switch (4f) {<br></div><div> satisfies int example -> example;<br></div><div> case float value -> (int) value;</div><div>};</div></blockquote><div><br></div></div><div>You could then open this "satisfies" behaviour to users, allowing them to define witness-like handlers. It would then be possible to test whether a string satisfies an int, which would make configuration parsing as simple as, for example:<br><br><blockquote style="border-left: 3px solid rgb(200, 200, 200); border-top-color: rgb(200, 200, 200); border-right-color: rgb(200, 200, 200); border-bottom-color: rgb(200, 200, 200); padding-left: 10px; color: rgb(102, 102, 102);"><div>int port = switch(config.getString("port")) {<br></div><div> satisfies int valid when valid >=0 && valid <= 65535 -> valid;<br></div><div> case String invalid -> // print to log and yield a default port<br></div><div> case null -> // yield default</div><div>};</div></blockquote><div><br></div></div><div>I believe this to be a clearer way to introduce the behaviour without polluting the meaning of instanceof.</div></div>
<div style="font-family: Arial, sans-serif; font-size: 14px;" class="protonmail_signature_block protonmail_signature_block-empty">
<div class="protonmail_signature_block-user protonmail_signature_block-empty"></div>
<div class="protonmail_signature_block-proton protonmail_signature_block-empty">
</div>
</div>