Submission: switch (...) instanceof feature

Rémi Forax forax at univ-mlv.fr
Mon May 4 03:18:54 PDT 2009


Reinier Zwitserloot a écrit :
> Why is this neccessary? We have a similar situation here as the  
> inherited exception issue: Rare and definitely broken (at runtime)  
> code will now break at compile time. This is technically 'backwards  
> incompatible', insofar that code that used to compile no longer does.  
> However, I believe the decision was made that this technical  
> incompatibility was acceptable, because the code made no runtime sense  
> in the first place. You have the same situation here:
>
>
> old code:
>
> Object x = foo();
>
> if ( x instanceof String ) {
>    Integer y = (Integer)x;
> }
>
> would compile, eventhough it's clearly bogus. With this change:
>
> if ( x instanceof String ) {
>     //within this block, 'x' is considered to be both a String and  
> whatever x's actual type is, if it's not a strict supertype of String.
> }
>   

This change is not backward compatible ...

class A {
  void m(String) { ... }
  void m(Object o) { ... }

  void f() {
    Object x = foo();
    if ( x instanceof String ) {
      m(x);      <--  call m(Object), will call m(String)
   }
 }
}

[ ... ]

>   --Reinier Zwitserloot
> Like it? Tip it!
> http://tipit.to
>   

Rémi



More information about the coin-dev mailing list