At this time, if you want to typecast an object to a derived class when the
object may legitimately not be an instance of the derived class, you have to do
this:
if (obj.class.isSubclass(DerivedClass))
derObj = DerivedClass.unsafeCast(obj);
The unsafeCast() avoids doing the check a second time in the cast() function.
In the interests of simplicity and of allowing users to avoid unsafe calls, we
need to introduce a flavor of cast() that does not abort (or throw an
exception, in the long term).
Current possibilities for this include:
Derived cast(Object object, Derived defaultValue);
# code above becomes
newObj = DerivedClass.cast(obj, null);
and:
Derived tryCast(Object object);
# code above becomes
newObj = DerivedClass.tryCast(obj);
Currently leaning towards the first form because it reduces the number of names
in the interface and allows us to use default values other than null (although
I currently can't think of a use-case for that).
Alternately, we could break compatibility and require a second boolean argument
indicating whether to abort/throw or return null.
Original issue reported on code.google.com by mind...@gmail.com on 14 Sep 2010 at 10:49
Original issue reported on code.google.com by
mind...@gmail.com
on 14 Sep 2010 at 10:49