Preconditions (for records, or otherwise)

Guy Steele guy.steele at oracle.com
Sat Mar 10 00:46:52 UTC 2018


> On Mar 9, 2018, at 6:24 PM, Brian Goetz <brian.goetz at oracle.com> wrote:
> 
> . . . That gets us down to:
> 
>  record Foo(int num, String unrelated) {
>    Foo(int num, String unrelated) {
>      if (num <= 0) ...;
>    }
>  }
> 
> Which is slightly better.  The remaining repetition is the argument list of the constructor is repeated.  On the one hand, this seems at first to be incompressible: Java members have signatures.  But if we're willing to do something special for records, we can take advantage of the fact that a record _must_ have a constructor (the "primary constructor") that matches the record signature, and let the user leave this out if they are declaring an explicit primary constructor:
> 
>  record Foo(int num, String unrelated) {
>    Foo {
>      if (num <= 0) ...;
>    }
>  }
> 
> or
> 
>  record Foo(int num, String unrelated) {
>    primary-constructor {
>      if (num <= 0) ...;
>    }
>  }
> 
> or similar could be a shorthand for declaring the full primary constructor signature.  I think that gets you down to your minimum, without losing much in the way of readability.  

Ya know, as long as we are talknig about a mandatory constructor, if that special keyword “primary-constructor” were spelled “required”, it would look achingly familiar:

 record Foo(int num, String unrelated) {
   required {
     if (num <= 0) ...;
   }
 }

Compare to:

 record Foo(int num, String unrelated)
   requires
     (num <= 0)
 {
 }

—Guy

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/amber-spec-experts/attachments/20180309/9e429625/attachment.html>


More information about the amber-spec-experts mailing list