[patterns] Nullability in patterns, and pattern-aware constructs (again)

Guy Steele guy.steele at oracle.com
Fri Jan 10 21:18:59 UTC 2020


> On Jan 10, 2020, at 4:00 PM, John Rose <john.r.rose at oracle.com> wrote:
> 
> On Jan 10, 2020, at 12:00 PM, Brian Goetz <brian.goetz at oracle.com> wrote:
>> . . .
> 
>> We might want to go a little further, and ban constant patterns in instanceof too, since all of the following have simpler forms:
>> 
>>    if (x instanceof null) { ... }
>>    if (x instanceof "") { ... }
>>    if (i instanceof 3) { ... }
>> 
>> Or not -- I suspect not.
> 
> One reason to keep it is floats:
> 
>  if (x instanceof Float.NaN) { … }
> 
> This seems to be a fine thing for an IDE to warn about, and not
> so fine for a language to legislate.

Well, if the _only_reason to allow instanceof with a constant is floats and doubles (comparing to NaN is the one thing that == or .equals might not handle properly), then it’s not a very good reason; I dare say Float.isNan(x) is the first thing that would jump to mind, and it would be a whole lot more readable as well.

—Guy



More information about the amber-spec-experts mailing list