Guard variable and being effectively final
forax at univ-mlv.fr
forax at univ-mlv.fr
Sat May 21 18:35:48 UTC 2022
----- Original Message -----
> From: "cay horstmann" <cay.horstmann at gmail.com>
> To: "Remi Forax" <forax at univ-mlv.fr>, "amber-spec-experts" <amber-spec-experts at openjdk.java.net>
> Sent: Saturday, May 21, 2022 7:50:44 PM
> Subject: Re: Guard variable and being effectively final
> Hi Rémy,
>
> it compiles with build 19-ea+23-1706 if you replace && with when.
sadly, assigning op also compiles :(
IntBinaryOperator op = null;
I think the new implementation is missing a check that the local variables after a "when" has to be effectively final
>
> Also, remove "4" from the list or add an operator :-)
yes
>
> Cheers,
>
> Cay
Rémi
>
> Il 21/05/2022 13:55, Remi Forax ha scritto:
>> interface reverse_polish_notation {
>> static Map<String, IntBinaryOperator> OPS =
>> Map.of("+", (a, b) -> a + b, "*", (a, b) -> a * b);
>>
>> static int eval(List<String> expr) {
>> var stack = new ArrayDeque<Integer>();
>> for(var token: expr) {
>> final IntBinaryOperator op;
>> stack.push(switch (token) {
>> case String __ && (op = OPS.get(token)) != null -> {
>> var value1 = stack.pop();
>> var value2 = stack.pop();
>> yield op.applyAsInt(value1, value2);
>> }
>> default -> Integer.parseInt(token);
>> });
>> }
>> return stack.pop();
>> }
>>
>> static void main(String[] args) {
>> var expr = List.of("1", "2", "+", "3", "*", "4");
>> System.out.println(eval(expr));
>> }
>> }
>
> --
>
> Cay S. Horstmann | http://horstmann.com | mailto:cay at horstmann.com
More information about the amber-spec-experts
mailing list