Guards -- not just for switch!

Kevin Bourrillion kevinb at
Wed Dec 6 19:55:25 UTC 2017

Sorry I'm on a thread necromancing kick.

I think this feature has value. Anything that encourages more aggressive
precondition checking is probably good.

I agree that `requires` would be a lot better than `where` for this.

I also feel that if we *don't* do something like this, then the design for
records has a serious problem, in that there is a huge cost involved to
adding a bit of validation to a record that didn't have any before. I think
that *must* be addressed, because we need *more* integrity checking in the
world, not less.

One other way to address it in records is with a @PostConstruct method
(where the fields were already set) instead of a call-through constructor,
but this `requires` seems more elegant and Java-ey.  (@PostConstruct is
part of JavaEE because it *feels* like an EE kind of thing.)

I will note it's more than a bit weird that code is getting executed from
what appears to be the signature of the method/class. Yes, it's like `final
int foo = executeWhateverHere()` ... but still feels quite weirder.

On Wed, Nov 29, 2017 at 7:24 PM, Tagir Valeev <amaembo at> wrote:

>     public Range(int lo, int hi)
>         where (lo <= hi) {
>             this.lo = lo;
>             this.hi = hi;
>     }
> Probably it's too early to argue about syntax, but now it's too similar to
> the 'while' loop. The 'where' keyword really looks like 'while' (same
> length, prefix and suffix) and just like the 'while' loop it's followed by
> parenthesized boolean expression and code block. I bet this would become a
> source of confusion when reading the code. The 'requires' keyword, as Remi
> suggests, sounds much better.
> With best regards,
> Tagir Valeev

Kevin Bourrillion | Java Librarian | Google, Inc. | kevinb at
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the amber-spec-experts mailing list