Why are arrays covariant but generics are invariant?

前端 未结 9 1263
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 00:44

From Effective Java by Joshua Bloch,

  1. Arrays differ from generic type in two important ways. First arrays are covariant. Generics are invariant.
  2. Cov

9条回答
  •  孤街浪徒
    2020-11-22 01:28

    Via wikipedia:

    Early versions of Java and C# did not include generics (a.k.a. parametric polymorphism).

    In such a setting, making arrays invariant rules out useful polymorphic programs. For example, consider writing a function to shuffle an array, or a function that tests two arrays for equality using the Object.equals method on the elements. The implementation does not depend on the exact type of element stored in the array, so it should be possible to write a single function that works on all types of arrays. It is easy to implement functions of type

    boolean equalArrays (Object[] a1, Object[] a2);
    void shuffleArray(Object[] a);
    

    However, if array types were treated as invariant, it would only be possible to call these functions on an array of exactly the type Object[]. One could not, for example, shuffle an array of strings.

    Therefore, both Java and C# treat array types covariantly. For instance, in C# string[] is a subtype of object[], and in Java String[] is a subtype of Object[].

    This answers the question "Why are arrays covariant?", or more accurately, "Why were arrays made covariant at the time?"

    When generics were introduced, they were purposefully not made covariant for reasons pointed out in this answer by Jon Skeet:

    No, a List is not a List. Consider what you can do with a List - you can add any animal to it... including a cat. Now, can you logically add a cat to a litter of puppies? Absolutely not.

    // Illegal code - because otherwise life would be Bad
    List dogs = new List();
    List animals = dogs; // Awooga awooga
    animals.add(new Cat());
    Dog dog = dogs.get(0); // This should be safe, right?
    

    Suddenly you have a very confused cat.

    The original motivation for making arrays covariant described in the wikipedia article didn't apply to generics because wildcards made the expression of covariance (and contravariance) possible, for example:

    boolean equalLists(List l1, List l2);
    void shuffleList(List l);
    

提交回复
热议问题