Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

java - Compilation error with generics and ternary operator in JDK 7

I ran into a compilation failure while writing some Java code, which I distilled down to the following test case:

import java.util.Collections;
import java.util.List;

public class TernaryFailure {
    public static List<String> thisWorks() {
        return Collections.emptyList();
    }

    public static List<String> thisFailsToCompile() {
        return true ? Collections.emptyList() : Collections.emptyList();
    }
}

The code above fails to compile with javac with JDK 1.7.0_45:

$ javac TernaryFailure.java
TernaryFailure.java:10: error: incompatible types
        return true ? Collections.emptyList() : Collections.emptyList();
                    ^
  required: List<String>
  found:    List<Object>
1 error

However, it compiles without any error with JDK 1.8.0_05.

Is that a bug in the Java 7 implementation? Or was there an enhancement to the Java Language Specification in Java 8 to start allowing this — and if so, what was the change?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The JLS SE 8 says at (§15.2):

When some expressions appear in certain contexts, they are considered poly expressions. The following forms of expressions may be poly expressions:

  • Parenthesized expressions (§15.8.5)

  • Class instance creation expressions (§15.9)

  • Method invocation expressions (§15.12)

  • Method reference expressions (§15.13)

  • Conditional expressions (§15.25)

  • Lambda expressions (§15.27)

So from this part of the spec is clear that conditional expressions, the ternary operator, can be considered poly expressions. But not all conditional expressions can be considered poly expressions, only reference conditional expressions according to (§15.25). The conditions under which a reference conditional expression can be considered a poly expression are clarified at (§15.25.3):

A reference conditional expression is a poly expression if it appears in an assignment context or an invocation context (§5.2. §5.3). Otherwise, it is a standalone expression.

Where a poly reference conditional expression appears in a context of a particular kind with target type T, its second and third operand expressions similarly appear in a context of the same kind with target type T.

The type of a poly reference conditional expression is the same as its target type.

Check that in your example the conditional expression appears in an assignment context because according to (§14.17):

When a return statement with an Expression appears in a method declaration, the Expression must be assignable (§5.2) to the declared return type of the method, or a compile-time error occurs.

So at the end of the day, what all this means? This implied that when conditional expressions are poly expressions, the target type is "pushed down" to each operand. This way the compiler can attribute each part of the condition, against the target. In your case the target is List<String>. If we check the definition of the emptyList() method we have:

@SuppressWarnings("unchecked")
public static final <T> List<T> emptyList() {
    return (List<T>) EMPTY_LIST;
}

So with the target List<String>, the compiler can infer that T == String and the code is accepted.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...