Simple and gloriously concise example (as opposed to mine!). Here, whether
the classes are native, or whether they are concrete instead of interfaces
won't change the need for a cast; all the involved objects have
commonalities but also important differences, and you want to treat them
differently. Refactoring (if it were possible) to support "proper"
polymorphism would obviously a bad idea, at least for me.

Cheers
Juan Pablo Califano
2010/6/8 Henrik Andersson <[email protected]>

> Juan Pablo Califano wrote:
>
>> I agree with you, but I'add for the sake of completeness that, sometimes,
>> relaxing the rules a bit becomes a necessary evil, like for instance, when
>> doing a cast.
>>
>>
> I have another example of where a typecast can be useful. When you really
> do need only BaseClass, but want to add some processing, if and only if, the
> object is of a specific DerivedClass.
>
> An examples of this example would be a display list crawler. You are just
> outputting all the objects, you need nothing other than DisplayObject for
> that. However, you want to go deep, so you use the "as" operator to get a
> reference to a DisplayObjectContainer. If it succeeds, you recurse. If not,
> not a problem.
>
> _______________________________________________
> Flashcoders mailing list
> [email protected]
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to