Expression switch - an alternate proposal
Ali Ebrahimi
ali.ebrahimi1781 at gmail.com
Fri Apr 13 08:19:59 UTC 2018
Hi,
On Fri, Apr 13, 2018 at 4:29 AM, Brian Goetz <brian.goetz at oracle.com> wrote:
> > Perhaps you shouldn't allow statements in a switch expression? They
> aren't allowed in a ? : expression either.
>
> Are you kidding? We’d get lynched. If you want to put one logging
> statement in one case of a 50 case switch, or construct a list and add to
> it, or have to call a setter on the thing you’re returning, or do something
> in a loop to build the result, you have to refactor the whole thing to a
> statement switch? We’d get laughed out of the room.
>
> Statements are a pain; no doubt. But we can’t hide our heads in the sand
> and pretend they don’t exist. We have to engage them as consistently and
> constructively as we can.
>
> > I also don't understand Rémy's remark that a traditional break inside a
> switch expression is disallowed. What rules out
> >
> > case BAKED_POTATO:
> > Toppings t = new Toppings();
> > t.add(SOUR_CREAM);
> > t.add(CHEESE);
> > int count = 0;
> > while (true) {
> > if (count == 10) break; else count++;
> > t.add(BACON);
> > }
> > break t;
>
> You can’t say “break” without a value in an expression switch, just like
> you can’t say “return” without a value in a value-bearing method.
>
I you mean above code is invalid, pardon Brain I tend to disagree with
decision. your rational is following code is invalid.
Function<String, String> outer =
x -> {
Consumer<String> inner = x ->
{ ... if(test) return, .... };
return x;
}
I think you better say that “break” without a value can not break an
expression switch if target of break is an expression switch.
--
Best Regards,
Ali Ebrahimi
More information about the amber-dev
mailing list