Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return outside of enclosing switch expression

I'm using a switch expression¹ in Java 12 to convert a string to a HTTP method:

static Optional<RequestMethod> parseRequestMethod(String methodStr) {
    return Optional.ofNullable(
          switch (methodStr.strip().toUpperCase(Locale.ROOT)) {
              case "GET" -> RequestMethod.GET;
              case "PUT" -> RequestMethod.PUT;
              case "POST" -> RequestMethod.POST;
              case "HEAD" -> RequestMethod.HEAD;

              default -> {
                  log.warn("Unsupported request method: '{}'", methodStr);
                  return null;
              }
          });
}

I'd like to warn about the unsupported method in the default branch and return null (which is then wrapped in an Optional).

But the code above causes a compiler error:

Return outside of enclosing switch expression

How do I get this to compile?


For completeness, here's the definition of the RequestMethod enum:

enum RequestMethod {GET, PUT, POST, HEAD}

¹ switch expressions were introduced in Java 12 as a preview feature.

like image 869
Matthias Braun Avatar asked Jun 28 '19 12:06

Matthias Braun


People also ask

Can I add return in switch case?

The Complete Full-Stack JavaScript Course!The JavaScript switch statement can contain return statements if it is present inside a function. The function will return the value in the switch statement and the code after the switch statement will not be executed.

Can I return in a switch statement Java?

Returning Inside switch Expressions. As a consequence of the distinction between switch statements and switch expressions, it is possible to return from inside a switch statement, but we're not allowed to do so from within a switch expression.

How many times switch () expression will be evaluated in Java?

Java SE 12 introduced switch expressions, which (like all expressions) evaluate to a single value, and can be used in statements.

How many times switch () expression will be evaluated?

The expression inside switch statement is invoked only one time.


1 Answers

use yield in Java 13

In Java 13, switch expressions use the new restricted identifier¹ yield to return a value from a block:

return Optional.ofNullable(
        switch (methodStr.strip().toUpperCase(Locale.ROOT)) {
            case "GET" -> RequestMethod.GET;
            // ... rest omitted

            default -> {
                log.warn("Unsupported request method: '{}'", methodStr);
                // yield instead of return
                yield null;
            }
        });

use break in Java 12

In Java 12, switch expressions use break to return a value from a block:

return Optional.ofNullable(
        switch (methodStr.strip().toUpperCase(Locale.ROOT)) {
            case "GET" -> RequestMethod.GET;
            // ... rest omitted

            default -> {
                log.warn("Unsupported request method: '{}'", methodStr);
                // break instead of return
                break null;
            }
        });

¹ yield is not a keyword, as helpfully pointed out by user skomisa.

like image 119
Matthias Braun Avatar answered Sep 27 '22 21:09

Matthias Braun