> On Aug 26, 2020, at 2:46 PM, Brian Goetz <brian.go...@oracle.com> wrote: > > >>> >>> Proposed: An `instanceof` expression must be able to evaluate to both true >>> and false, otherwise it is invalid. This rules out strongly total patterns >>> on the RHS. If you have a strongly total pattern, use pattern assignment >>> instead. >> >> Makes sense to me, but one question: would this restriction "must be able to >> evaluate to both true and falseā be applied to _every_ `instanceof` >> expression, or only those that have a pattern to right of the `instanceof` >> keyword? I ask because if it is applied to _every_ `instanceof` expression, >> this would represent an incompatible change to the behavior of `x instanceof >> Object`, among others. Is it indeed the intent to make an incompatible >> change to the language? > > Well, it's more of an aspiration than a rule -- based on what we already do. > We already use some form of this to rule out bad casts: > > String s = ... > if (s instanceof Integer) { ... } > // Error, incompatible types > > So here, we have a candidate instanceof that would always be false, which the > compiler can derive by type analysis, and was always rejected. It would be > joined by > > if (s instanceof Object o) { ... } > > because Object o is total in this case, but not > > if (s instanceof Object) > > because this _can_ yield either true (if s is not null) or false (if it is.)
Thank you: I had missed this point about null. Duh.