PM design question: Scopes
Brian Goetz
brian.goetz at oracle.com
Sat Nov 4 21:31:18 UTC 2017
> On Nov 4, 2017, at 4:46 PM, Mark Raynsford <mark at io7m.com> wrote:
>
> On 2017-11-03T10:44:51 +0000
> Gavin Bierman <gavin.bierman at oracle.com> wrote:
>>
>> switch (o) {
>> case int i : ... // in scope and DA
>> ... // in scope and DA
>> case T i : // int i not in scope, so can re-use
>> }
>>
>
> I'm strongly in favour of this one. In my experience and opinion,
> fallthrough was a mistake in C and it was a mistake to copy it in Java.
While you will find few fans of fall through around here, let’s not forget that (a) we have it, and (b) the scoping of variables in switch is already reflective of this — the scope of a local declared in a switch extends to the end of the switch. So we’re not going to be able to roll back either of these decisions for existing (primitive, string, box, enum) switches. Further, the more we can define the rules so that we don’t divide the language into “old switch” and “new switch”, the more pattern switch looks like a natural extension of the existing language rather than a bifurcation.
> In some 15 years and close to a million lines of code, I have never once
> felt the need to fall through a switch case.
That’s true for a lot of developers, but not true for all. The JDK, for example, contains hundreds of switch fallthroughs. I went through a lot of them as part of this project, to see what the usage was, and while some of them are dumb, most are not. They show up often in parsers, for example. But, the farther you move “up the stack”, the rarer they are, so they will be a lot more appropriate when switching on chars than on objects. I have no problem saying “OR patterns (repeated case labels) are all we’ll need for patterns” (and that’s really the only thing that makes sense for expression switch anyway.)
What I take from your mail is that you have two real goals here:
- Discouraging fall through;
- Allowing re-use of pattern binding labels in a switch.
But this particular scoping rule is not the only way to get to either of these.
> From a pedagogical standpoint, all of the other languages I know of
> that have implemented pattern matching never had any kind of
> fallthrough in the first place, so it'd likely benefit Java to match
> them (no pun intended!). I'm thinking of people who learned pattern
> matching via something like ML being able to write Java
> switches/matches without having to make the mental gear change of
> inserting these annoying "break" statements everywhere.
You’ll get this with expression switch, but I don’t realistically see how we can do this for statement switch, unless we want to create a new syntactic form for “match.” Which is back to “bifurcation.”
> Is there actually a significant amount of code out there that uses
> switch case fall through?
Sadly yes.
More information about the amber-spec-experts
mailing list