Yield as contextual keyword (was: Call for bikeshed -- break replacement in expression switch)

Brian Goetz brian.goetz at oracle.com
Mon May 20 16:16:23 UTC 2019


> But I now think that this argument is weak, because I believe it is in fact unlikely that there will be a large number of other statement types that we will want to turn into expressions.
> 
> 	* Yes, we may want block expressions.
> 	* We would want `if`-statement expressions _except_ that we already have the ternary operator ? :, so I don’t really think there is a pressing need.
> 	* As a Common Lisp programmer, I can see the value of having a `for` loop produce a value.  But I very much doubt that Joe Java-programmer wants that.
> 	* And if we don’t want values from `for` loops, I doubt there will be much demand for values from `while` loops or `try` statements.

While I agree about if / for / while, I’m not completely convinced on `try`; the notion of “produce a value or throw” is a common one, and has all the same advantages over a try statement that a switch expression has over a switch statement — that initialization of variables with side-effectful statements is messier and more error-prone than with expressions.  So, for example, the not-so-uncommon idiom: 

    static final Foo foo;
    static { 
        try {
            foo = new Foo();
       }
       catch (FooException fe) {
           throw new OtherKindOfException(fe);
       }
    }

leaves, well, room for improvement.  Let’s just register that as something we might want to do someday.

Another form of expression that might have statements in it is some sort of “let” or “with” expression; it is not uncommon enough to have to execute statements to produce a result:

    Foo f = new Foo();
    f.setBar(1);
    return f;

which similarly would like to be replaced with an expression.  (let’s not design this here, the point is that this is a road we might well want to walk again some day.)




More information about the amber-spec-observers mailing list