[Pitch] if and switch expressions

Second suggestion: Consider stealing Java’s solution to multi-statement branches.

Java’s switch expressions introduce a new keyword that essentially means “return from enclosing switch,” and it’s…not too bad:

var response = switch (utterance) {
    case "thank you" -> "you’re welcome";
    case "atchoo" -> "gesundheit";
    case "fire!" -> {
        log.warn("fire detected");
        yield "everybody out!";  // yield = value of multi-statement branch
    };
    default -> "who knows";
}

I’m not sure yield is a good keyword choice for Swift, since it smells like a language that could one day have coroutines, but the introduction of a new keyword that is not return for this purpose seems like a promising direction.

3 Likes