<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1252">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<tt>That would also be OK, as the two different versions of the rule
are not very far apart in practice -- nearly all switches that
will take a broader argument type will also want to do something
more interesting with it (type test, deconstruct, etc.) <br>
<br>
</tt><br>
<div class="moz-cite-prefix">On 12/14/2017 2:42 PM, Guy Steele
wrote:<br>
</div>
<blockquote type="cite"
cite="mid:2518601F-A2C0-4B7E-BF66-5584A453FF9B@oracle.com">
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1252">
I think this is the right approach.
<div class=""><br class="">
</div>
<div class="">An alternative that should at least be considered
would be to further simplify the statement of the exception by
eliminating the mention of legacy types:</div>
<div class=""><br class="">
</div>
<div class=""> - For switches which have only constant case labels
(and "default"), we relax the above rule regarding default. </div>
<div class=""><br class="">
</div>
<div class=""><br class="">
<div>
<blockquote type="cite" class="">
<div class="">On Dec 14, 2017, at 2:39 PM, Brian Goetz <<a
href="mailto:brian.goetz@oracle.com" class=""
moz-do-not-send="true">brian.goetz@oracle.com</a>>
wrote:</div>
<br class="Apple-interchange-newline">
<div class="">
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1252" class="">
<div text="#000000" bgcolor="#FFFFFF" class=""> <tt
class="">Here's where I think we should come down on
this. <br class="">
<br class="">
First, we should appeal to the general rule about
pattern ordering; if pattern X dominates Y (that is,
everything that matches Y also matches X), X can't
come before Y. <br class="">
<br class="">
This gives us:<br class="">
- "case null" must precede any non-constant case
label<br class="">
- "default" must come at the end.<br class="">
<br class="">
Now, we carve out special dispensation for existing
switches:<br class="">
- For switches whose static argument type is one of
the legacy switch types (primitives, boxes, string,
enum) *and* which have only constant case labels (and
"default"), we relax the above rule regarding
default. <br class="">
<br class="">
<br class="">
</tt><br class="">
<div class="moz-cite-prefix">On 12/11/2017 2:15 PM,
Brian Goetz wrote:<br class="">
</div>
<blockquote type="cite"
cite="mid:ca9fe87a-5ae2-3670-386f-33cd1065fb9d@oracle.com"
class="">
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1252" class="">
<tt class="">The middle is surely awful. Though in
the JDK, we have a fair number of uses where default
is the _first_ case, which isn't unreasonable, and
some might argue is even clearer in some cases. <br
class="">
<br class="">
The reason to tread lightly on forcing
reorganization of existing switches is that it is
allowable to fall into *and out of* the default
case. So if someone has:<br class="">
<br class="">
switch (x) {<br class="">
default: S; // fall through<br class="">
case COMMON: T; break;<br class="">
case UNCOMMON: U; break;<br class="">
}<br class="">
<br class="">
then eventually getting to an error when default is
not last for "legacy" switches (those where all
labels are type-restating constants) means some
uncomfortable refactoring just to "make the compiler
happy." So while I agree on warnings, I'm not sure
if we can ever get to error in all cases without
picking some fights with users. <br class="">
<br class="">
<br class="">
</tt><br class="">
<div class="moz-cite-prefix">On 12/11/2017 1:16 PM,
Kevin Bourrillion wrote:<br class="">
</div>
<blockquote type="cite"
cite="mid:CAGKkBkuo7Zp0p2i=QBYBE3W1WX1f-XdwTwEe5x-_k3Y-eFjbVw@mail.gmail.com"
class="">
<div dir="ltr" class="">
<div class="gmail_extra">
<div class="gmail_quote">On Fri, Nov 3, 2017 at
5:25 PM, Brian Goetz <span dir="ltr" class=""><<a
href="mailto:brian.goetz@oracle.com"
target="_blank" moz-do-not-send="true"
class="">brian.goetz@oracle.com</a>></span>
wrote:<br class="">
<blockquote class="gmail_quote"
style="margin:0 0 0 .8ex;border-left:1px
#ccc solid;padding-left:1ex"><br class="">
or plan to eventually get to a place where
default always comes last, even for "int"
switches. If we want to get to the latter,
we should start warning on this construct
now.</blockquote>
<div class=""><br class="">
</div>
<div class="">I favor starting to warn and
eventually forbidding default in any
position but last for all constructs that
have it.</div>
<div class=""><br class="">
</div>
<div class="">A switch with the default in the
middle is extremely weird and confusing. If
I'm reading code to understand what happens
when i == 3, and I read as far as</div>
<div class=""><br class="">
</div>
</div>
</div>
<blockquote style="margin:0 0 0
40px;border:none;padding:0px" class="">
<div class="gmail_extra">
<div class="gmail_quote">
<div class="">switch (i) {</div>
</div>
</div>
<div class="gmail_extra">
<div class="gmail_quote">
<div class=""> case 1:</div>
</div>
</div>
<div class="gmail_extra">
<div class="gmail_quote">
<div class=""> justOneStuff(); break();</div>
</div>
</div>
<div class="gmail_extra">
<div class="gmail_quote">
<div class=""> case 2:</div>
</div>
</div>
<div class="gmail_extra">
<div class="gmail_quote">
<div class=""> justTwoStuff(); break();</div>
</div>
</div>
<div class="gmail_extra">
<div class="gmail_quote">
<div class=""> default:</div>
</div>
</div>
</blockquote>
<div class="gmail_extra">
<div class="gmail_quote">
<div class=""> </div>
<div class="">... then I immediately assume
that this must be where execution is
continuing. Worse, even if I do notice that
there are more case labels to follow, and I
resume searching for a `case 3:`, then when
I don't find one I now risk making <i
class="">another</i> error and forgetting
to jump <i class="">back</i> to the default.</div>
<div class=""><br class="">
</div>
<div class="">This is kind of insane. At first
I was less worried because I thought "surely
no one is actually doing this"... then I
browsed our codebase.... yikes.</div>
<div class=""><br class="">
</div>
<div class="">We should at least strongly
consider this.</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<blockquote class="gmail_quote"
style="margin:0 0 0 .8ex;border-left:1px
#ccc solid;padding-left:1ex">
<div class="m_2816769939487245128HOEnZb">
<div class="m_2816769939487245128h5"> On
11/3/2017 5:10 PM, Tagir Valeev wrote:<br
class="">
<blockquote class="gmail_quote"
style="margin:0 0 0
.8ex;border-left:1px #ccc
solid;padding-left:1ex"> Hello!<br
class="">
<br class="">
Currently the default branch can be
placed in any place inside the<br
class="">
switch operator, e.g. like this:<br
class="">
<br class="">
switch(i) {<br class="">
case 1: System.out.println("one");brea<wbr
class="">k;<br class="">
default:
System.out.println("other");br<wbr
class="">eak;<br class="">
case 2: System.out.println("two");brea<wbr
class="">k;<br class="">
}<br class="">
<br class="">
In this case behavior does not change
on the order of case blocks.<br
class="">
However in pattern matching the order
of cases usually matters: if<br
class="">
some pattern matches, this means that
the subsequent patterns will not<br
class="">
be checked. Does this mean that with
pattern matching the default<br
class="">
branch makes all the subsequent case
blocks unreachable? Or default<br
class="">
can still be located anywhere and is
checked only after any other<br
class="">
pattern?<br class="">
<br class="">
With best regards,<br class="">
Tagir Valeev<br class="">
</blockquote>
<br class="">
</div>
</div>
</blockquote>
</div>
<br class="">
<br class="" clear="all">
<div class=""><br class="">
</div>
-- <br class="">
<div
class="m_2816769939487245128gmail_signature"
data-smartmail="gmail_signature">
<div dir="ltr" class="">
<div class="">
<div dir="ltr" class="">
<div class="">
<div dir="ltr" class="">
<div
style="line-height:1.5em;padding-top:10px;margin-top:10px;color:rgb(85,85,85);font-family:sans-serif"
class=""><span
style="border-width:2px 0px
0px;border-style:solid;border-color:rgb(213,15,37);padding-top:2px;margin-top:2px"
class="">Kevin Bourrillion |</span><span
style="border-width:2px 0px
0px;border-style:solid;border-color:rgb(51,105,232);padding-top:2px;margin-top:2px"
class=""> Java Librarian |</span><span
style="border-width:2px 0px
0px;border-style:solid;border-color:rgb(0,153,57);padding-top:2px;margin-top:2px"
class=""> Google, Inc. |</span><span
style="border-width:2px 0px
0px;border-style:solid;border-color:rgb(238,178,17);padding-top:2px;margin-top:2px"
class=""> <a
href="mailto:kevinb@google.com"
target="_blank"
moz-do-not-send="true"
class="">kevinb@google.com</a></span></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
<br class="">
</blockquote>
<br class="">
</div>
</div>
</blockquote>
</div>
<br class="">
</div>
</blockquote>
<br>
</body>
</html>