Tech Off Post

Single Post Permalink

View Thread: Generic Type Casting - Unsolveable?
  • User profile image

    Thanks for the feedback so far. You're right that an interface would solve it if it was about modifying the Bs.

    But the truth is that the core logic doesn't really care about anything B implements. It only wants to create the business objects (dynamically creating Bs, but working with them as As) and put them into the list.

    The calling component however knows that it does not just get any list from the core but a list of Bs, so it thinks it should be possible to actually get a typed list of Bs back from the core.

    I realize that this is more of a convenience issue, but isn't generic typing all about preventing coding errors through type safety? If I get a list of As back and only I know that they are all Bs, that doesn't measure up to that safety.

    BTW: What really puzzles me about all that business is that Array implicitely has this functionality (B[] is not really derived from A[], but still the 'as' operator works) - while it seems to be impossible to replicate this behaviour in my own code.