JEP325: Switch expressions spec

Kevin Bourrillion kevinb at google.com
Fri May 11 12:35:38 UTC 2018


I think it is right to be more concerned with the long-term outlook than
with any temporary transitional pains (except of course for the inescapable
compatibility requirement).



On Fri, May 11, 2018 at 2:44 AM, Gavin Bierman <gavin.bierman at oracle.com>
wrote:

> You make the point very well Guy. This is what makes non-clean-room
> language design so hard! I think the question is where we put our weight.
> Clearly we could say we worry more about people migrating existing code, in
> which case we would elect to support migrating from : to -> and the
> simplest rule is to support case p1 -> case p2 -> ….
>
> The other view, and the one we had when designing this, was that once we
> ship the -> form, everyone will immediate use this all the time. Moreover,
> this is the one that will be in taught in all the Java 101 classes. In
> these courses, at the end of the switch lecture, the teacher will say: if
> you really need fall through. then change all your -> to :, insert some
> breaks etc. In this scenario the requirement is different: it’s to support
> case p1, p2:
>
> So the design decision is to either put our weight behind people migrating
> old switches to new with a simple rule, or behind people writing new
> switches and discovering in a handful of cases they need fall through and
> having to move to : form with a simple rule.
>
> Gavin
>
>
> > On 10 May 2018, at 03:43, Guy Steele <guy.steele at oracle.com> wrote:
> >
> > I completely agree with John’s careful analysis, including the more
> speculative parts about symbol connotations.
> >
> > I too find "case B -> case L -> S” distasteful.  But on the other hand I
> also find ”case B: case L: S” distasteful.
> >
> > What I find acceptable is either
> >
> >       case B ->
> >       case L -> S
> >
> > or
> >
> >       case B:
> >       case L: S
> >
> > As a matter of style I strongly prefer the keyword “case” to appear only
> at the start of a line.
> > This makes them easier to spot when reading code.
> >
> > What I am arguing for is to have a choice: when you want to put more
> than one pattern on a line, by all means use the keyword “case” just once
> and separate the patterns with commas.  But if you want to string them out
> vertically, I would like to have the option of repeating the “case” keyword
> once per line, whether using colons or arrows.
> >
> > And I argue that the latter style leads to more readable code in the
> “case null / default” situation; I prefer
> >
> >       case null ->
> >       default -> E
> >
> > to
> >
> >       case null, default -> E
> >
> > because I don’t like to bury that “default” keyword—I think it merits
> the same start-of-line visibility as the “case” keyword.  In addition, it
> really bugs me to treat “default” as if it were syntactically a pattern—it
> isn’t.
> >
> > However, if others prefer "case null, default -> E”, I would be okay
> with supporting that also.   But I would not want to write it.
> >
> > There is a tension or tradeoff between (a) the difference in
> connotations of arrow and colon, and (b) the simplicity of the theory that
> in principle they are more or less interchangeable syntactically, and we
> should make conversion from one to the other as easy as possible.  I may
> value (b) more than other people do.
> >
> > And now, having laid out my complete case (so to speak), I think I am
> done with arguing this, and am perfectly prepared to be outvoted or
> overruled.
> >
> > —Guy
> >
> >
> >> On May 9, 2018, at 10:20 PM, John Rose <john.r.rose at oracle.com> wrote:
> >>
> >> A quick $0.02 on one bit of the switch syntax-alooza:
> >>
> >> I tried hard to like Guy's theory that -> and : are two flavors
> >> of the same SwitchLabel syntax.  It is the most economical
> >> way to spread the good parts from colonform to arrowform,
> >> including the need (on occasion) to label one of the switch
> >> clauses as the "default", even while it *also* has a "case N".
> >> But I agree with others who find it too strange looking.
> >>
> >> If we can't buy Guy's clean syntax idea, we *do* need an
> >> ad hoc way to combine multiple cases in arrowform and
> >> an *even more* ad hoc way to fold in "default".
> >> And of all the possibilities, I think Brian's (below) is
> >> the least surprising and most acceptable.  Please,
> >> let's not drop the ball and damage arrowform clauses
> >> by forbidding multiple ClauseLabel inputs for them.
> >>
> >> Here's my take on formalizing Brian's suggestion:
> >>
> >> CasePattern:
> >> case Pattern { , Pattern } [ , default ]
> >> default
> >>
> >> (Note that default comes last in the CasePattern, no
> >> matter what.)
> >>
> >> That's $0.0199.  The rest of it is why I think I can't like
> >> Guy's proposal.  The colonform and arrowform just look
> >> too different; colons and arrows have drastically different
> >> connotations.  Arrow says "go from here to there" (e.g.,
> >> from lambda parameter to lambda result).  Colon says,
> >> "I'm telling you something about the following thing."
> >> Chained arrows seem to say something like "I'm going
> >> to breakfast, then to lunch, then to supper", not
> >> "red and blue are the colors of this shoe", as colons
> >> would.
> >>
> >> I know that's really subjective, but I think *something* like
> >> that, some folk model or perception, is behind people's
> >> distaste for "case B -> case L -> S" when the same people
> >> are content with "case R: case B: S".  It's not that arrow
> >> is heavier than colon, exactly; it's that arrow really means
> >> something different than colon.
> >>
> >> At least, arrow and colon differ in the context of Java.
> >> In some parts of some languages "a:b" does mean
> >> "from a to b".  But in those places "a:b:c" doesn't mean
> >> "from a or b to c" as in Java.  In any case, "a -> b -> c"
> >> never means "from either a or b to c", as Guy's syntactic
> >> deductions would lead us to try in Java.
> >>
> >> — John
> >>
> >> On Apr 23, 2018, at 11:20 AM, Guy Steele <guy.steele at oracle.com> wrote:
> >>>
> >>> I argue that there is no need to make the special-case exception for
> `default`.  When you need to play that game (usually because null needs to
> be addressed), you cannot use the comma-separation syntax.  Instead, just
> say either
> >>>
> >>>     case null: default: s;                  (or, if you prefer,
> `default: case null: s;`)
> >>>
> >>> or
> >>>
> >>>     case null -> default -> s;              (or, if you prefer,
> `default -> case null -> s;`)
> >>
> >> On Apr 20, 2018, at 11:40 AM, Brian Goetz <Brian.Goetz at Oracle.COM>
> wrote:
> >>>
> >>> One thing that is relevant to the short term is that now that we
> killed mixed labels, we'd have to have a way to say "case null or default"
> in arrow world.  The least stupid thing seems to be to allow default to be
> tacked on to a comma-separated case list as if it were a pattern:
> >>>
> >>>   case A -> s1;
> >>>   case null, default -> s2;
> >>>
> >>> since you can no longer say:
> >>>
> >>>   case A -> s1;
> >>>   case null:
> >>>   default:
> >>>       s2;
> >>>
> >>
> >>
> >
>
>


-- 
Kevin Bourrillion | Java Librarian | Google, Inc. | kevinb at google.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/amber-spec-experts/attachments/20180511/3f88043a/attachment.html>


More information about the amber-spec-experts mailing list