Compact Record Constructors

Tagir Valeev amaembo at gmail.com
Mon Nov 11 14:40:22 UTC 2019


пн, 11 нояб. 2019 г., 20:59 Jordan Zimmerman <jordan at jordanzimmerman.com>:

> Excuse a comment from the peanut gallery - we already have class
> initializers so why are "Compact Record Constructors" even needed? Wouldn't
> a non-static class initializer do the same thing? E.g.
>
> record Pair(String name, int value) {
>     {
>         Objects.requireNonNull(name);
>     }
> }
>

I thought about this possibility. It's not the same in a number of ways:
- compact constructor is only one, but you may have several initializers
- unlike initializers, compact constructor has parameters
- you can explicitly call compact constructor from another constructor
supplying custom arguments via this(...) statement. This cannot be done
with initializer.

So to me it seems not right to use the same syntax for the thing which is
semantically different in so many ways.

With best regards,
Tagir Valeev


More information about the amber-spec-observers mailing list