Submission: switch (...) instanceof feature
Gabriel Belingueres
belingueres at gmail.com
Mon May 4 07:27:54 PDT 2009
touché!
Maybe it is time to consider an idiom like C++'s dynamic_cast?
void f() {
Object x = foo();
String s = dynamic_cast<String>(x);
if (s != null) {
m(s); // call m(String)
m(x); // call m(Object);
}
}
2009/5/4 Rémi Forax <forax at univ-mlv.fr>:
> 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