"satisfies" behaviour within switch cases (JEP 530)
Protonull
protonull at protonmail.com
Tue Feb 10 07:32:18 UTC 2026
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:
> if (0.5f satisfies int example) // false
> if (1f satisfies int example) // true
This keyword would be used in lieu of "instanceof" and "case", like so:
> int result = switch (4f) {
> satisfies int example -> example;
> case float value -> (int) value;
> };
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:
> int port = switch(config.getString("port")) {
> satisfies int valid when valid >=0 && valid <= 65535 -> valid;
> case String invalid -> // print to log and yield a default port
> case null -> // yield default
> };
I believe this to be a clearer way to introduce the behaviour without polluting the meaning of instanceof.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20260210/57fa2bfa/attachment-0001.htm>
More information about the amber-dev
mailing list