Re: switch statement and lambda

2019-04-09 Thread forax
> De: "Brian Goetz" 
> À: "Remi Forax" 
> Cc: "Gavin Bierman" , "amber-spec-experts"
> 
> Envoyé: Mardi 9 Avril 2019 21:14:25
> Objet: Re: switch statement and lambda

> 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.
seems wise, ok ! 

Rémi 

>> On Apr 9, 2019, at 3:10 PM, [ mailto:fo...@univ-mlv.fr | fo...@univ-mlv.fr ]
>> wrote:

>>> De: "Gavin Bierman" < [ mailto:gavin.bier...@oracle.com |
>>> gavin.bier...@oracle.com ] >
>>> À: "Remi Forax" < [ mailto:fo...@univ-mlv.fr | fo...@univ-mlv.fr ] >
>>> Cc: "amber-spec-experts" < [ mailto:amber-spec-experts@openjdk.java.net |
>>> amber-spec-experts@openjdk.java.net ] >
>>> Envoyé: Mardi 9 Avril 2019 19:28:57
>>> Objet: Re: switch statement and lambda

>>>> On 6 Apr 2019, at 21:17, Remi Forax < [ mailto:fo...@univ-mlv.fr |
>>>> fo...@univ-mlv.fr ] > wrote:

>>>> Currently this code doesn't compile
>>>> IntConsumer c = x -> switch(x) { default -> System.out.println(x); };

>>>> I believe it should because this is the basic pattern for supporting the 
>>>> actor
>>>> model,
>>>> you consume a message and do a side effect* depending on the type of the
>>>> message,
>>>> translated in Java, you want a lambda that takes a message as parameter, 
>>>> calls a
>>>> switch to do the pattern matching and return void.

>>> 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:

>>> • If the function type's result is void, the lambda body is either a 
>>> statement
>>> expression (§14.8) or a void-compatible block.

>>> Which means that the following code typechecks:

>>> IntConsumer ic = x -> System.out.println(x);

>>> but it breaks as soon as we nest the statement expression, e.g.

>>> IntConsumer ic2 = x -> true ? System.out.println(x) : 
>>> System.out.println(x); //
>>> Compilation error: target-type for conditional expression cannot be void
>>> 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.

>> yes,
>> 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.

>> By example, this does not compile
>> [ https://github.com/fora | https://github.com/fora ]
>> x/loom-fiber/blob/cea7b86c26c2e86b00fb72e5098a37983e8b6441/src/main/java/fr.umlv.loom/fr/umlv/loom/actor/CounterStringActorExprSwitchDemo.java#L17
>> while this compiles
>> [
>> https://github.com/forax/loom-fiber/blob/6d8a0a6ba870580d43988cd0d507d19d57653d62/src/main/java/fr.umlv.loom/fr/umlv/loom/actor/CounterStringActorExprSwitchDemo.java#L17
>> |
>> https://github.com/forax/loom-fiber/blob/6d8a0a6ba870580d43988cd0d507d19d57653d62/src/main/java/fr.umlv.loom/fr/umlv/loom/actor/CounterStringActorExprSwitchDemo.java#L17
>> ]

>>> Cheers,
>>> Gavin

>> cheers,
>> Rémi


Re: switch statement and lambda

2019-04-09 Thread Brian Goetz
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. 


> On Apr 9, 2019, at 3:10 PM, fo...@univ-mlv.fr wrote:
> 
> 
> 
> De: "Gavin Bierman" 
> À: "Remi Forax" 
> Cc: "amber-spec-experts" 
> Envoyé: Mardi 9 Avril 2019 19:28:57
> Objet: Re: switch statement and lambda
> 
> On 6 Apr 2019, at 21:17, Remi Forax  <mailto:fo...@univ-mlv.fr>> wrote:
> 
> Currently this code doesn't compile
>  IntConsumer c = x -> switch(x) { default -> System.out.println(x); };
> 
> I believe it should because this is the basic pattern for supporting the 
> actor model,
> you consume a message and do a side effect* depending on the type of the 
> message,
> translated in Java, you want a lambda that takes a message as parameter, 
> calls a switch to do the pattern matching and return void.
> 
> 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:
> 
> • If the function type's result is void, the lambda body is either a 
> statement expression (§14.8) or a void-compatible block.
> 
> Which means that the following code typechecks:
> 
> IntConsumer ic = x -> System.out.println(x);
> 
> but it breaks as soon as we nest the statement expression, e.g.
> 
> IntConsumer ic2 = x -> true ? System.out.println(x) : System.out.println(x); 
> // Compilation error: target-type for conditional expression cannot be void
> 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.
> 
> yes,
> 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.
> 
> By example, this does not compile
> https://github.com/fora 
> <https://github.com/fora>x/loom-fiber/blob/cea7b86c26c2e86b00fb72e5098a37983e8b6441/src/main/java/fr.umlv.loom/fr/umlv/loom/actor/CounterStringActorExprSwitchDemo.java#L17
> while this compiles
> https://github.com/forax/loom-fiber/blob/6d8a0a6ba870580d43988cd0d507d19d57653d62/src/main/java/fr.umlv.loom/fr/umlv/loom/actor/CounterStringActorExprSwitchDemo.java#L17
>  
> <https://github.com/forax/loom-fiber/blob/6d8a0a6ba870580d43988cd0d507d19d57653d62/src/main/java/fr.umlv.loom/fr/umlv/loom/actor/CounterStringActorExprSwitchDemo.java#L17>
> 
> 
> 
> Cheers,
> Gavin
> 
> cheers,
> Rémi



Re: switch statement and lambda

2019-04-09 Thread forax
> De: "Gavin Bierman" 
> À: "Remi Forax" 
> Cc: "amber-spec-experts" 
> Envoyé: Mardi 9 Avril 2019 19:28:57
> Objet: Re: switch statement and lambda

>> On 6 Apr 2019, at 21:17, Remi Forax < [ mailto:fo...@univ-mlv.fr |
>> fo...@univ-mlv.fr ] > wrote:

>> Currently this code doesn't compile
>> IntConsumer c = x -> switch(x) { default -> System.out.println(x); };

>> I believe it should because this is the basic pattern for supporting the 
>> actor
>> model,
>> you consume a message and do a side effect* depending on the type of the
>> message,
>> translated in Java, you want a lambda that takes a message as parameter, 
>> calls a
>> switch to do the pattern matching and return void.

> 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:

> • If the function type's result is void, the lambda body is either a statement
> expression (§14.8) or a void-compatible block.

> Which means that the following code typechecks:

> IntConsumer ic = x -> System.out.println(x);

> but it breaks as soon as we nest the statement expression, e.g.

> IntConsumer ic2 = x -> true ? System.out.println(x) : System.out.println(x); 
> //
> Compilation error: target-type for conditional expression cannot be void
> 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.

yes, 
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. 

By example, this does not compile 
[ https://github.com/fora | https://github.com/fora ] 
x/loom-fiber/blob/cea7b86c26c2e86b00fb72e5098a37983e8b6441/src/main/java/fr.umlv.loom/fr/umlv/loom/actor/CounterStringActorExprSwitchDemo.java#L17
 
while this compiles 
https://github.com/forax/loom-fiber/blob/6d8a0a6ba870580d43988cd0d507d19d57653d62/src/main/java/fr.umlv.loom/fr/umlv/loom/actor/CounterStringActorExprSwitchDemo.java#L17
 

> Cheers,
> Gavin

cheers, 
Rémi 


Re: switch statement and lambda

2019-04-09 Thread Gavin Bierman

> On 6 Apr 2019, at 21:17, Remi Forax  wrote:
> 
> Currently this code doesn't compile
>  IntConsumer c = x -> switch(x) { default -> System.out.println(x); };
> 
> I believe it should because this is the basic pattern for supporting the 
> actor model,
> you consume a message and do a side effect* depending on the type of the 
> message,
> translated in Java, you want a lambda that takes a message as parameter, 
> calls a switch to do the pattern matching and return void.

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:

• If the function type's result is void, the lambda body is either a 
statement expression (§14.8) or a void-compatible block.

Which means that the following code typechecks:

IntConsumer ic = x -> System.out.println(x);

but it breaks as soon as we nest the statement expression, e.g.

IntConsumer ic2 = x -> true ? System.out.println(x) : System.out.println(x); // 
Compilation error: target-type for conditional expression cannot be void
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.

Cheers,
Gavin




switch statement and lambda

2019-04-06 Thread Remi Forax
Currently this code doesn't compile
  IntConsumer c = x -> switch(x) { default -> System.out.println(x); };

I believe it should because this is the basic pattern for supporting the actor 
model,
you consume a message and do a side effect* depending on the type of the 
message,
translated in Java, you want a lambda that takes a message as parameter, calls 
a switch to do the pattern matching and return void.
 
The other reason is that it's not rare to move from a switch expression to a 
switch statement and vice-versa when developing the code,
and adding/removing a pair of curly braces around the code you are writing 
because it's the body/expression of a lambda is not very user friendly.

regards,
Rémi

* it's cleaner in Erlang because you have tail calls, so the side effects are 
hidden.