<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">I see why this is tempting, but I am going to suggest we wait.  As part of Valhalla, we would like for `void` to become a real type some day; that will require evaluating all the places in the JLS where we treat statement and expressions differently, or make exceptions like this.  Until we’ve completed this analysis, I’m reluctant to add more special cases.  <div class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Apr 9, 2019, at 3:10 PM, <a href="mailto:forax@univ-mlv.fr" class="">forax@univ-mlv.fr</a> wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="caret-color: rgb(0, 0, 0); font-family: arial, helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><br class="Apple-interchange-newline"><br class=""></div><hr id="zwchr" data-marker="__DIVIDER__" style="caret-color: rgb(0, 0, 0); font-family: arial, helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><div data-marker="__HEADERS__" style="caret-color: rgb(0, 0, 0); font-family: arial, helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><blockquote style="border-left-width: 2px; border-left-style: solid; border-left-color: rgb(16, 16, 255); margin-left: 5px; padding-left: 5px; font-weight: normal; font-style: normal; text-decoration: none; font-family: Helvetica, Arial, sans-serif; font-size: 12pt;" class=""><b class="">De:<span class="Apple-converted-space"> </span></b>"Gavin Bierman" <<a href="mailto:gavin.bierman@oracle.com" class="">gavin.bierman@oracle.com</a>><br class=""><b class="">À:<span class="Apple-converted-space"> </span></b>"Remi Forax" <<a href="mailto:forax@univ-mlv.fr" class="">forax@univ-mlv.fr</a>><br class=""><b class="">Cc:<span class="Apple-converted-space"> </span></b>"amber-spec-experts" <<a href="mailto:amber-spec-experts@openjdk.java.net" class="">amber-spec-experts@openjdk.java.net</a>><br class=""><b class="">Envoyé:<span class="Apple-converted-space"> </span></b>Mardi 9 Avril 2019 19:28:57<br class=""><b class="">Objet:<span class="Apple-converted-space"> </span></b>Re: switch statement and lambda<br class=""></blockquote></div><div data-marker="__QUOTED_TEXT__" style="caret-color: rgb(0, 0, 0); font-family: arial, helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><blockquote style="border-left-width: 2px; border-left-style: solid; border-left-color: rgb(16, 16, 255); margin-left: 5px; padding-left: 5px; font-weight: normal; font-style: normal; text-decoration: none; font-family: Helvetica, Arial, sans-serif; font-size: 12pt;" class=""><br class=""><div class=""><blockquote class=""><div class="">On 6 Apr 2019, at 21:17, Remi Forax <<a href="mailto:forax@univ-mlv.fr" class="" target="_blank">forax@univ-mlv.fr</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">Currently this code doesn't compile<br class=""> IntConsumer c = x -> switch(x) { default -> System.out.println(x); };<br class=""><br class="">I believe it should because this is the basic pattern for supporting the actor model,<br class="">you consume a message and do a side effect* depending on the type of the message,<br class="">translated in Java, you want a lambda that takes a message as parameter, calls a switch to do the pattern matching and return void.<br class=""></div></div></blockquote><div class=""><br class=""></div><div class="">I understand, although this is actually to do with the way lambda expressions are typed, rather than the switch expression. In JLS 15.27.3 "Type of a Lambda Expression”, there is a special case:</div><div class=""><br class=""></div><div class=""><div class="">• If the function type's result is void, the lambda body is either a statement expression (§14.8) or a void-compatible block.</div></div><div class=""><br class=""></div><div class="">Which means that the following code typechecks:</div><div class=""><br class=""></div><div class=""><div class=""><pre class="" style="background-color: rgb(255, 255, 255); font-family: Menlo; font-size: 10.5pt;">IntConsumer ic = x -> System.out.println(x);</pre><div class=""><br class=""></div></div><div class="">but it breaks as soon as we nest the statement expression, e.g.</div><div class=""><br class=""></div><div class=""><pre class="" style="background-color: rgb(255, 255, 255); font-family: Menlo; font-size: 10.5pt;">IntConsumer ic2 = x -> <span class="" style="color: rgb(0, 0, 128); font-weight: bold;">true </span>? System.out.println(x) : System.out.println(x); // Compilation error: target-type for conditional expression cannot be void</pre></div><div class="">This is what is happening in your example. So to deal with this we’d either have to make typechecking of lambdas smarter - either by replacing the typing rule for lambdas above with something more compositional, or by making void a first-class type, or we could perhaps add a pattern-matching form of lambda, which has a void-aware typing rule. I’m not sure about any of these options for now.</div></div></div></blockquote><div class=""><br class=""></div><div class="">yes,<br class=""></div><div class="">i'm proposing to create a special case for a switch inside a lambda expression for the same reason we have a special treatment for methods.<br data-mce-bogus="1" class=""></div><div class=""><br data-mce-bogus="1" class=""></div><div class="">By example, this does not compile<br data-mce-bogus="1" class=""></div><div class=""><a href="https://github.com/fora" class="">https://github.com/fora</a>x/loom-fiber/blob/cea7b86c26c2e86b00fb72e5098a37983e8b6441/src/main/java/fr.umlv.loom/fr/umlv/loom/actor/CounterStringActorExprSwitchDemo.java#L17</div><div class="">while this compiles<br data-mce-bogus="1" class=""></div><div class=""><a href="https://github.com/forax/loom-fiber/blob/6d8a0a6ba870580d43988cd0d507d19d57653d62/src/main/java/fr.umlv.loom/fr/umlv/loom/actor/CounterStringActorExprSwitchDemo.java#L17" class="">https://github.com/forax/loom-fiber/blob/6d8a0a6ba870580d43988cd0d507d19d57653d62/src/main/java/fr.umlv.loom/fr/umlv/loom/actor/CounterStringActorExprSwitchDemo.java#L17</a><br data-mce-bogus="1" class=""></div><div class=""><br data-mce-bogus="1" class=""></div><div class=""><br data-mce-bogus="1" class=""></div><blockquote style="border-left-width: 2px; border-left-style: solid; border-left-color: rgb(16, 16, 255); margin-left: 5px; padding-left: 5px; font-weight: normal; font-style: normal; text-decoration: none; font-family: Helvetica, Arial, sans-serif; font-size: 12pt;" class=""><div class=""><div class=""><div class=""><br class=""></div><div class="">Cheers,</div><div class="">Gavin</div></div></div></blockquote><div class=""><br class=""></div><div class="">cheers,<br data-mce-bogus="1" class=""></div><div class="">Rémi</div></div></div></blockquote></div><br class=""></div></body></html>