Proposal: single-expression method bodies

Gavin Bierman gavin.bierman at oracle.com
Mon Nov 27 10:25:20 UTC 2017


> On 25 Nov 2017, at 03:58, Daniel Trebbien <dtrebbien at gmail.com> wrote:
> 
> Hello Zheka,
> 
> I personally like your idea of supporting expression lambda syntax with
> methods.  Being able to write simple getters as public int getSomeField()
> -> someField; would be very useful, in my opinion.

+1

> I don't like the idea of expression lambda syntax support for switch cases,
> though, because I think that it wouldn't work well with fall-through
> support.  By way of example, consider:
> 
> String s = switch (o) {
>    case Integer n:
>    case Double n:
>        return n.toString();
> }
> 
> (I believe that reusing bound variable names like this is anticipated to be
> supported, but I could be wrong.)

I don’t think fall-through makes sense for expression switches. What you really want is an OR pattern, e.g. case P1, P1 -> e, which means “if it matches pattern P1 *or* pattern P2”. We have plans for this. Of course, the concrete syntax can be argued about… whatever is chosen will look a little odd to start with, but I would hope we’d get used to it fairly quickly.

Note also that OR patterns make sense even for conventional switch.

Gavin

PS: Reusing variable names is surely useful, e.g. case Point(int x, 0), Point(int x, 1) -> …, but I think we need to be careful. Really what we are doing is introducing a new “x” in the body of the clause that is like a merge point of the two pattern occurrences. My personal preference is that the types should be identical, but one could imagine e.g. taking LUB of the types.


More information about the amber-dev mailing list