Java thenComparing wildcard signature

前端 未结 3 1753
广开言路
广开言路 2021-02-05 18:48

Why does the declaration look like this:

default > Comparator thenComparing(
            Function

        
3条回答
  •  离开以前
    2021-02-05 19:15

    It seems like your question is regarding type arguments in general so for my answer I will be separating the type arguments you provided from the types they belong to, in my answer, for simplicity.

    First we should note that a parameterized type of wildcard is unable to access its members that are of the respective type parameter. This is why, in your specific case the ? extends U can be substituted for U and still work fine.

    This won't work in every case. The type argument U does not have the versatility and additional type safety that ? extends U has. Wildcards are a unique type argument in which instantiations of the parameterized types (with wildcard type arguments) are not as restricted by the type argument as they would be if the type argument was a concrete type or type parameter; wildcards are basically place holders that are more general than type parameters and concrete types (when used as type arguments). The first sentence in the java tutorial on wild cards reads:

    In generic code, the question mark (?), called the wildcard, represents an unknown type.

    To illustrate this point take a look at this

    class A  {}
    

    now let's make two declarations of this class, one with a concrete type and the other with a wild card and then we'll instantiate them

    A  aConcrete = new A (); // Compile time error
    A  aWild = new A() // Works fine
    

    So that should illustrate how a wildcard type argument does not restrict the instantiation as much as a concrete type. But what about a type parameter? The problem with using type parameters is best manifested in a method. To illustrate examine this class:

    class C  {
        void parameterMethod(A a) {}
        void wildMethod(A a) {}
        void test() {
            C  c = new C();
            A a = new A();
            c.parameterMethod(a); // Compile time error
            c.wildMethod(a); // Works fine
        }
    

    Notice how the references c and a are concrete types. Now this was addressed in another answer, but what wasn't addressed in the other answer is how the concept of type arguments relate to the compile time error(why one type argument causes a compile time error and the other doesn't) and this relation is the reason why the declaration in question is declared with the syntax it's declared with. And that relation is the additional type safety and versatility wildcards provide over type parameters and NOT some typing convention. Now to illustrate this point we will have to give A a member of type parameter, so:

    class A { T something; }
    

    The danger of using a type parameter in the parameterMethod() is that the type parameter can be referred to in the form of a cast, which enables access to the something member.

    class C {
        parameterMethod(A a) { a.something = (U) "Hi"; }
    }
    

    Which in turn enables the possibility of heap pollution. With this implementation of the parameterMethod the statement C c = new C(); in the test() method could cause heap pollution. For this reason, the compiler issues a compile time error when methods with arguments of type parameter are passed any object without a cast from within the type parameters declaring class; equally a member of type parameter will issue a compile time error if it is instantiated to any Object without a cast from within the type parameter's declaring class. The really important thing here to stress is without a cast because you can still pass objects to a method with an argument of type parameter but it must be cast to that type parameter (or in this case, cast to the type containing the type parameter). In my example

        void test() {
            C  c = new C();
            A a = new A();
            c.parameterMethod(a); // Compile time error
            c.wildMethod(a); // Works fine
        }
    

    the c.parameterMethod(a) would work if a were cast to A, so if the line looked like this c.parameterMethod((A) a); no compile time error would occur, but you would get a run time castclassexection error if you tried to set an int variable equal to a.something after the parameterMethod() is called (and again, the compiler requires the cast because U could represent anything). This whole scenario would look like this:

        void test() {
            C  c = new C();
            A a = new A();
            c.parameterMethod((A) a); // No compile time error cuz of cast
            int x = a.something; // doesn't issue compile time error and will cause run-time ClassCastException error
        }
    

    So because a type parameter can be referenced in the form of a cast, it is illegal to pass an object from within the type parameters declaring class to a method with an argument of a type parameter or containing a type parameter. A wildcard cannot be referenced in the form of a cast, so the a in wildMethod(A a) could not access the T member of A; because of this additional type safety, because this possibility of heap pollution is avoided with a wildcard, the java compiler does permit a concrete type being passed to the wildMethod without a cast when invoked by the reference c in C c = new C(); equally, this is why a parameterized type of wildcard can be instantiated to a concrete type without a cast. When I say versatility of type arguments, I'm talking about what instantiations they permit in their role of a parameterized type; and when I say additional type safety I'm talking about about the inability to reference wildcards in the form of a cast which circumvents heapPollution.

    I don't know why someone would cast a type parameter. But I do know a developer would at least enjoy the versatility of wildcards vs a type parameter. I may have written this confusingly, or perhaps misunderstood your question, your question seems to me to be about type arguments in general instead of this specific declaration. Also if keyExtractor from the declaration Function keyExtractor is being used in a way that the members belonging to Function of the second type parameter are never accessed, then again, wildcards are ideal because they can't possibly access those members anyway; so why wouldn't a developer want the versatility mentioned here that wildcards provide? It's only a benefit.

提交回复
热议问题