Please add support for explicit casts with 'as' keyword

Topics: Language Specification
Oct 3, 2012 at 7:32 PM

It is sometimes necessary to let the compiler know that a downcast is intended and therefore should not trigger compile time errors. For example:

class Animal {...}
class Snake extends Animal {...}
class Horse extends Animal {...}
function foo( animal : Animal )
{ 
  if( animal.name == "Snake" ) 
  { 
    var s : Snake = animal as Snake; 
    ...
  }
}

Thanks!

 

 

Oct 3, 2012 at 7:47 PM

You can use a type assertion. Consider:

var s = <Snake>animal;

Oct 3, 2012 at 7:54 PM

Oh, I didn't know that - just found it in the documentation!

In other languages 's' becomes null if 'animal' is not a 'Snake' without throwing any errors.

But type assertions would work for me!

Please close this thread.

Thanks,

- Bernd