다음을 통해 공유


Generics and Wildcards

Martin write a post entitled Generics: The Importance of Wildcards

He concludes with:

Unfortunately, C# will not support wildcards or a similar mechanism. The implementation strategy does not allow the introduction of wildcards (generics are implemented in the runtime instead of by type erasure). This is a bit surprising, since the implementation strategy is often claimed to be superior. What disappoints me is that the designers of C# are not willing to admit that subtyping is an issue and that wildcards are a solution.

That's a very binary statement, when in fact the implementation of generics both in the .NET and Java worlds are exercises in tradeoffs.

I agree that subtyping is an issue, and that wildcards are *a* solution (I don't know enough to know whether it is the only solution, or whether a solution that worked only on reference types would be interesting.). But, having looked at the implementation of wildcards in Java, they add a fair amount of complexity to the language, and I'm not sure that the added flexibility that they give you is important enough to add language complexity. I do know that when I was talking to C# users, the ability to do this was not one of the things that came up.

When I weigh the speculative benefits there against the known benefits of the .NET implementation - collections of primitive types, for example - it's pretty clear which one I would prefer.

Comments

  • Anonymous
    May 26, 2005
    The comment has been removed

  • Anonymous
    May 26, 2005
    The comment has been removed

  • Anonymous
    May 27, 2005
    The comment has been removed

  • Anonymous
    May 27, 2005
    The Java implementation isn't "seriously broken" by a long shot -- but it is seriously hairy. I played around with each beta as this was being developed, and believe me, there are a lot of dark corners. Some interesting possibilities (ie, wildcards and proper array variance) didn't end up making the Java 1.5 release because they were just a little too hairy to be confident in, given the release time frame.

    I think there is likely some improvement in error messages to be done (the compiler should have suggested the variant type the second poster provided, given the erroneous code of the first poster, etc), but the theory is certainly solid.

  • Anonymous
    June 07, 2009
    PingBack from http://greenteafatburner.info/story.php?id=5296

  • Anonymous
    June 09, 2009
    PingBack from http://quickdietsite.info/story.php?id=5242