Call for feedback -- switch expressions in JDK 12

Brian Goetz brian.goetz at oracle.com
Sat Apr 27 22:56:06 UTC 2019


I’m not sure whether you’re asking about the specific overloading of -> (some people find it disturbing that both switch and lambda use the same punctuation; others see a switch as a sort of “function by parts” and the arrow makes sense to them), or about why there was a need to have a shorthand form at all.  

We surely could have done this:

     case A: break-with 1;
     case B: break-with 2; 
    …

And that might have been a reasonable idea, though people seem to have such an allergy to “break” in switch that the convenience factor was significant.


> One more item in the feedback list:
> 
> We understand that the "->" in switch expressions lends itself to future
> pattern matching constructs, but is there a real need for "->" to be there
> in switch statements as well other than the convenience factor of avoiding
> break ( or rather making the break implicit)  in switch statements?
> 
> Regards,
> Manoj
> 
> 
> 
> From:	"Manoj Palat" <manoj.palat at in.ibm.com>
> To:	"Alex Buckley" <alex.buckley at oracle.com>
> Cc:	Stephan Herrmann <stephan.herrmann at berlin.de>,
>            jdk-dev at openjdk.java.net
> Date:	04/19/2019 03:28 AM
> Subject:	Re: Call for feedback -- switch expressions in JDK 12
> Sent by:	"jdk-dev" <jdk-dev-bounces at openjdk.java.net>
> 
> 
> 
> 
> Hi Alex,
> Please find our feedback from Eclipse Java Dev.
> 
>   1)		 For the break expression part we would be happy to have the
>      “overloading” of break replaced by something else ? since this is
>      already in the cards, no additional comment on this part.
> 
>   2)		 Thanks to Remi Forax raising a bug in ecj [
> 
> https://urldefense.proofpoint.com/v2/url?u=https-3A__bugs.eclipse.org_bugs_show-5Fbug.cgi-3Fid-3D545716&d=DwIFJg&c=jf_iaSHvJObTbx-siA1ZOg&r=A51zLdywEjS50U7u2UMWvsDIrUGEJ5IDXskL5MxIEjA&m=kii1fGYS4Td8yanneEhpKyvY68-2ZuWy6UGnDArvEtU&s=bTz3kPNJIWSaFf9MBqxF1ZKUwUHxpH0AJkS2Noth9O0&e=
> ] and the
>      following discussions, we believe that there could be a bug [in spec,
>      javac as well as ecj] ? see this comment from Stephan Herrmann
> 
> https://urldefense.proofpoint.com/v2/url?u=https-3A__bugs.eclipse.org_bugs_show-5Fbug.cgi-3Fid-3D545716-23c16&d=DwIFJg&c=jf_iaSHvJObTbx-siA1ZOg&r=A51zLdywEjS50U7u2UMWvsDIrUGEJ5IDXskL5MxIEjA&m=kii1fGYS4Td8yanneEhpKyvY68-2ZuWy6UGnDArvEtU&s=lp0V33xF-hHx6PbOuSpmziGYZGad42PPROmWzFvu8xQ&e=
> specifically
>      for the findings. [@Alex: I am taking the liberty of pointing to the
>      bug link rather than summarizing here since you have mentioned that a
>      blog pointer should also be fine]
> 
>   Regards,
> 
>   Manoj
> 
>   Eclipse Java Dev, IBM
> 
> 
> 
> 
> From:		 Alex Buckley <alex.buckley at oracle.com>
> To:		 jdk-dev at openjdk.java.net
> Date:		 04/17/2019 11:23 PM
> Subject:		 Re: Call for feedback -- switch expressions in JDK 12
> Sent by:		 "jdk-dev" <jdk-dev-bounces at openjdk.java.net>
> 
> 
> 
> The feedback we're looking for is "I tried switch expressions in my code
> and observed ...". For example:
> 
> https://urldefense.proofpoint.com/v2/url?u=https-3A__mail.openjdk.java.net_pipermail_amber-2Ddev_2019-2DMarch_004199.html&d=DwIFaQ&c=jf_iaSHvJObTbx-siA1ZOg&r=A51zLdywEjS50U7u2UMWvsDIrUGEJ5IDXskL5MxIEjA&m=yINx0-noxNSSsm-dLx2P9YD7dvF6187FW7CStT07mp4&s=9at0fZB0bQpeEPI5L2IWbMSlUqcdgEsfF5944Rfjaxo&e=
> 
> 
> 
> If anyone is blogging about their experience with switch expressions,
> feel free to share the URL here. For example, here's a great writeup of
> using switch expressions in lambdas:
> 
> https://urldefense.proofpoint.com/v2/url?u=http-3A__minborgsjavapot.blogspot.com_2019_03_java-2D12-2Dmapping-2Dwith-2Dswitch-2Dexpressions.html&d=DwIFaQ&c=jf_iaSHvJObTbx-siA1ZOg&r=A51zLdywEjS50U7u2UMWvsDIrUGEJ5IDXskL5MxIEjA&m=yINx0-noxNSSsm-dLx2P9YD7dvF6187FW7CStT07mp4&s=JE0ESlf3gLQJ5-QzuOU1uR_xMLngiysxuz0o948ZlW4&e=
> 
> 
> 
> Alex
> 
> On 4/16/2019 8:24 PM, Netroby wrote:
>>    int numLetters = switch (day) {
>>        case MONDAY, FRIDAY, SUNDAY -> 6,
>>        case TUESDAY                -> 7,
>>        case THURSDAY, SATURDAY     -> 8,
>>        case WEDNESDAY              -> 9,
>>        _ -> 0,
>>    };
>> 
>> Should we using  comma , instead of ;
>> 
>> comma , means these cases the same switch.
>> 
>> Another point, should we have an default match pattern ? using _ or
> default
>> 
>> 
>> idea from rust match
>> ```rust
>> fn main() {
>>     let number = 13;
>>     // TODO ^ Try different values for `number`
>> 
>>     println!("Tell me about {}", number);
>>     match number {
>>         // Match a single value
>>         1 => println!("One!"),
>>         // Match several values
>>         2 | 3 | 5 | 7 | 11 => println!("This is a prime"),
>>         // Match an inclusive range
>>         13...19 => println!("A teen"),
>>         // Handle the rest of cases
>>         _ => println!("Ain't special"),
>>     }
>> 
>>     let boolean = true;
>>     // Match is an expression too
>>     let binary = match boolean {
>>         // The arms of a match must cover all the possible values
>>         false => 0,
>>         true => 1,
>>         // TODO ^ Try commenting out one of these arms
>>     };
>> 
>>     println!("{} -> {}", boolean, binary);
>> }
>> 
>> ```
>> 
>> Appreciate your time.
>> ----------------------------
>> Netroby
>> 
>> Alex Buckley <alex.buckley at oracle.com> 于2019年4月17日周三 上午10:00写
> 道:
>>> 
>>> An easy way to help move Java forward is to try out new features on real
>>> code and share your experiences. We're asking for feedback on how you
>>> use switch expressions, the new language feature in JDK 12:
>>> 
>>>    int numLetters = switch (day) {
>>>        case MONDAY, FRIDAY, SUNDAY -> 6;
>>>        case TUESDAY                -> 7;
>>>        case THURSDAY, SATURDAY     -> 8;
>>>        case WEDNESDAY              -> 9;
>>>    };
>>> 
>>>    // Multiple labels per case (also allowed in switch statements)
>>>    // No fallthrough with the -> form (also allowed in switch
> statements)
>>> 
>>> Do you find switch expressions useful? Is anything surprising? Did you
>>> turn any switch statements into switch expressions? Please let us know
>>> on this list, even if the answer is "Tried it. Works fine."
>>> 
>>> To enable switch expressions in your environment:
>>> - IntelliJ IDEA 2019.1
>>> 
> https://urldefense.proofpoint.com/v2/url?u=https-3A__blog.jetbrains.com_idea_2019_02_java-2D12-2Dand-2Dintellij-2Didea_&d=DwIFaQ&c=jf_iaSHvJObTbx-siA1ZOg&r=A51zLdywEjS50U7u2UMWvsDIrUGEJ5IDXskL5MxIEjA&m=yINx0-noxNSSsm-dLx2P9YD7dvF6187FW7CStT07mp4&s=-WsqDARpX60gLIBem_6IqycbQFRWL5bzkTNGVV322aU&e=
> 
> 
>>> - Eclipse 4.11
> https://urldefense.proofpoint.com/v2/url?u=https-3A__www.eclipse.org_eclipse_news_4.11_jdt.php-23Java12&d=DwIFaQ&c=jf_iaSHvJObTbx-siA1ZOg&r=A51zLdywEjS50U7u2UMWvsDIrUGEJ5IDXskL5MxIEjA&m=yINx0-noxNSSsm-dLx2P9YD7dvF6187FW7CStT07mp4&s=zClnCpCg04gt1vZS1Zz8j64uam6ZkpnGMVe4M5kCYd0&e=
> 
> 
>>> - Apache NetBeans 11.0
>>> 
> https://urldefense.proofpoint.com/v2/url?u=https-3A__cwiki.apache.org_confluence_pages_viewpage.action-3FpageId-3D103091452&d=DwIFaQ&c=jf_iaSHvJObTbx-siA1ZOg&r=A51zLdywEjS50U7u2UMWvsDIrUGEJ5IDXskL5MxIEjA&m=yINx0-noxNSSsm-dLx2P9YD7dvF6187FW7CStT07mp4&s=5RLv6qs5qiU3r9dJ8KjnT4aPKRyz8hxXvNN0w5Z6x94&e=
> 
> 
>>> - jshell in JDK 12  run with `--enable-preview`
>>> - javac in JDK 12  run with `--release 12 --enable-preview`
>>> 
>>> Alex
> 
> 
> 
> 
> 
> 



More information about the jdk-dev mailing list