Call for feedback -- switch expressions in JDK 12

Netroby hufeng1987 at gmail.com
Wed Apr 17 03:24:49 UTC 2019


   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://blog.jetbrains.com/idea/2019/02/java-12-and-intellij-idea/
> - Eclipse 4.11  https://www.eclipse.org/eclipse/news/4.11/jdt.php#Java12
> - Apache NetBeans 11.0
> https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=103091452
> - 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