Assignment of final fields outside of constructors

Pedro Lamarão pedro.lamarao at prodist.com.br
Fri Jul 7 13:27:48 UTC 2023


Em qui., 6 de jul. de 2023 às 21:18, Nir Lisker <nlisker at gmail.com>
escreveu:


> I remember some discussion for "nested methods": allowing methods (and
> constructors) to contain other methods; I assume that that would be one way
> to solve it.
>

Now that  we have lambdas, many "nested method" solutions are already
possible:

class Boat {
    final int sails;
    Boat(Type type) {
        Supplier<int> supplier = () -> {
            return switch (type) {
                case ENGINE ->  0;
                case SAIL -> 2;
                default -> 1;
            }
        };
        this.sails = supplier.get();
    }
}

-- 
Pedro Lamarão
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20230707/f969a8c8/attachment.htm>


More information about the amber-dev mailing list