----- Mail original ----- > De: "Guy Steele" <guy.ste...@oracle.com> > À: "Brian Goetz" <brian.go...@oracle.com> > Cc: "amber-spec-experts" <amber-spec-experts@openjdk.java.net> > Envoyé: Mercredi 29 Novembre 2017 22:49:23 > Objet: Re: Guards -- not just for switch!
> Um, let’s be careful here. See below. > >> On Nov 29, 2017, at 4:54 PM, Brian Goetz <brian.go...@oracle.com> wrote: >> >> As we've swirled around the design space on pattern matching, it seems the >> sensible place to land is to not provide explicit syntax for AND and OR >> patterns, but to support guards on case labels: >> >> case Foo(String s) >> where (s.length() > 0): ... >> >> In the course of a separate discussion, we realized that guards can >> profitably >> go in some other places, too. Like methods/constructors: >> >> public Range(int lo, int hi) >> where (lo <= hi) { >> this.lo = lo; >> this.hi = hi; >> } >> > > The two situations are not analogous, because if a case clause fails you just > try the next one, but if a constructor fails you don’t try another > constructor. > > If we use the term “where” on a constructor, then Joe Programmer (that's me) i was thinking you were more a random Guy :) > will probably be very surprised if > > switch (x) { > case Foo(String s) > where (s.length() > 0): ... > case Foo(String s) > where (s.length() == 0): ... > } > > > works, but > > public Interval(int lo, int hi) > where (lo <= hi) { > this.lo = lo; > this.hi = hi; > this.exterior = false; > } > > public Interval(int lo, int hi) > where (lo > hi) { > this.lo = lo; > this.hi = hi; > this.exterior = true; > } > > does not work. > > It might make sense to use a word such as “requires” or “assert” rather than > “where” in the constructor case. yes, it's the whole design by contract thingy, i vote for "requires" like in Eiffel, record Apple(int seed) requires seed >= 0; > > —Guy Rémi