Atom Feed SITE FEED   ADD TO GOOGLE READER

I want !instanceof

I'm about a year late for project coin. But I still want an obvious Java language feature: !instanceof. To illustrate:
  public void equals(Object other) {
if (other !instanceof Foo) {
return false;
}
return ((Foo) other).bar.equals(bar);
}

With the instanceof operator, null instanceof T is false for all values of T. Symmetrically, I suppose null !instanceof T should always be true.
Well, a little better than

if (!(other instanceof Foo))
Agreed. I've been using false == x instanceof Y to avoid the double parens.
I prefer the getClass() route for equals:
if (that != null || that.getClass() != getClass()) ...

In this context, instanceof violates the contract of equals, allowing a situation where a.equals(b) is true but b.equals(a) is false if b is an instance of a subclass and a an instance of the superclass.
@Rory not true; List.equals() is only correct when implemented with instanceof.
Damn where were you? I agree, this is annoying as hell.
@swankjesse: That's because the contract for List#equals() is different to that for Object#equals() in such a way as to forbid subclasses from messing up symmetry the way they can for non-List Objects.

Still, I know there have been times when I've wanted to type !instanceof, whatever approach one usually favours for equals().
I've always written (x instanceof String == false).

But then I never use ! to negate anyway, I think its much less readable than == false.