Dechenjm / crack-language

Automatically exported from code.google.com/p/crack-language
Other
0 stars 0 forks source link

cast() needs a non-abortive overload #37

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
Implemented in rev 78b8f979ba30

Original comment by mind...@gmail.com on 22 Aug 2012 at 11:40