1

I'm studying switch expression, and I'd like to know how to pass control to the invoker method because "yield" just exits from the switch expression. I can't find any way to make it behave like the classic switch statement.

Is it even possible?

UPDATE: Here's a little piece of code.

public static boolean isTimeToParty(DayOfWeek day) {
    boolean isParty = switch (day) {
        case MONDAY -> false;
        case TUESDAY -> {
            yield false; //return not allowed
        }
        case WEDNESDAY -> false;
        case THURSDAY -> false;
        case FRIDAY -> false;
        case SATURDAY -> true;
        case SUNDAY -> true;
    };
    return isParty;
}

What I mean is: How to avoid assigning value to a variable and then calling return [variable_name]? I would like to return value and exit switch at the same time.

Thanks in advance

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Unearthly
  • 121
  • 6
  • 2
    This is a little vague. What's an "invoker method?" If you're referring to the caller, you can simply `return`. – Robert Harvey Nov 14 '21 at 22:33
  • 2
    You can also show us some code that illustrates what you're talking about. – Robert Harvey Nov 14 '21 at 22:34
  • What is the difference between "pass control to the invoker method" and "exits from the switch expression"? Exiting from the switch is exactly what `break` does in "classic" switch expressions (i.e. switch statements). – sprinter Nov 14 '21 at 23:35
  • The same as `...?... :... ` the switch _expression_ cannot return. It could have been allowed like `if (...) return false; yield true;` but really, that is awful. – Joop Eggen Nov 15 '21 at 00:23
  • 2
    Since the switch expression is an...expression, would typing it like: "return switch expression" do the trick? I tried on the IDE and it's working, or am I missing something? – Unearthly Nov 15 '21 at 08:53
  • @Unearthly Correct: you cannot put `return` inside the switch. In your case it is easier to just use `return switch (day) { ...` to avoid the assignment to `isParty`. – DuncG Nov 15 '21 at 10:41
  • thanks a lot for the feedback! My question I think it's now answered then :) – Unearthly Nov 15 '21 at 11:18

2 Answers2

2

Simply return to the calling method. If you just want to stop processing further cases in the same switch, use break. This and the below example apply to the switch statement, whereas the original question was releated to the switch expression.

public static void main(String[] args) throws Exception {

    switch(1) {
        case 1:
            System.out.println("Case one");
            // no break, so case processing goes one
        case 2:
            System.out.println("Case two");
            break; // we bail out here and do not process further cases
        default:
            System.out.println("Default case");
    }
    
    switch(2) {
        case 2:
            return;
        default:
            System.out.println("Default case");
    }
    
    System.out.println("This is the end - should not be printed");
}

generates output

Case one
Case two
Queeg
  • 7,748
  • 1
  • 16
  • 42
2

As you cannot use return or break within the switch, it would be easier in your case to simplify with return switch ... directly:

public static boolean isTimeToParty(DayOfWeek day) {
    return switch (day) {
        case SATURDAY, SUNDAY -> true;
        default  -> false;
    };
}
DuncG
  • 12,137
  • 2
  • 21
  • 33
  • How come one cannot use return or break inside switch? I added examples that do. – Queeg Nov 15 '21 at 11:08
  • 2
    @Hiran Chaudhuri Your examples are the original form of switch statements - they allow break + return and cannot be assigned to a local variable. A switch expression however uses `->` or `yield` inside each case - every case must hand back a result of same type as the others so that the yielded value can be assigned into a local variable: `boolean isParty = switch(day) ...` – DuncG Nov 15 '21 at 11:14
  • Thanks for pointing out the difference between switch statement and expression. I will emphasize that in my response. – Queeg Nov 15 '21 at 11:48