Submission: switch (...) instanceof feature

howard.lovatt at gmail.com howard.lovatt at gmail.com
Wed Apr 22 02:54:28 PDT 2009


Another example would be:

class MyClass {

private int x;

public boolean equals( final Object other ) {
if ( other == null ) { return false; }
switch ( other ) instanceof {
case MyClass: return x == other.x;
}
return false;
}

}

You can also use the construct like case statements in functional  
languages, eg how Scala uses its case classes. If this, switch on  
instanceof, is done for switch it should probably be done for if as well,  
ie:

public boolean equals( final Object other ) {
if ( other == null ) { return false; }
if ( other instanceof MyClass ) { return x == other.x; }
return false;
}

and probably, but might be too hard, on equality tests of class as well as  
instanceof test, eg:

public boolean equals( final Object other ) {
if ( other == null ) { return false; }
if ( other.getClass() == MyClass.class ) { return x == other.x; } //  
equality, rather than instanceof
return false;
}

and:

public boolean equals( final Object other ) {
if ( other == null ) { return false; }
switch ( other.getClass() ) { // equality, rather than instanceof
case MyClass: return x == other.x;
}
return false;
}

These features, as described above, are in the language Nice where it was  
claimed they eliminated the need for casts completely. Eliminating casts  
would be a good result!



More information about the coin-dev mailing list