10.12.2012 Views

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

EXPRESSIONS Compile-Time Step 2: Determine Method Signature 15.12.2<br />

15.12.2.7 Inferring Type Arguments Based on Actual Arguments<br />

In this section, we describe the process of inferring type arguments for method<br />

and constructor invocations. This process is invoked as a subroutine when testing<br />

for method (or constructor) applicability (§15.12.2.2 - §15.12.2.4).<br />

DISCUSSION<br />

<strong>The</strong> process of type inference is inherently complex. <strong>The</strong>refore, it is useful to give an informal<br />

overview of the process before delving into the detailed specification.<br />

Inference begins with an initial set of constraints. Generally, the constraints require that<br />

the statically known types of the actual arguments are acceptable given the declared formal<br />

argument types. We discuss the meaning of “acceptable” below.<br />

Given these initial constraints, one may derive a set of supertype and/or equality constraints<br />

on the formal type parameters of the method or constructor.<br />

Next, one must try and find a solution that satisfies the constraints on the type parameters.<br />

As a first approximation, if a type parameter is constrained by an equality constraint,<br />

then that constraint gives its solution. Bear in mind that the constraint may equate one type<br />

parameter with another, and only when the entire set of constraints on all type variables is<br />

resolved will we have a solution.<br />

A supertype constraint T:>Ximplies that the solution is one of supertypes of X. Given<br />

several such constraints on T, we can intersect the sets of supertypes implied by each of<br />

the constraints, since the type parameter must be a member of all of them. We can then<br />

choose the most specific type that is in the intersection.<br />

Computing the intersection is more complicated than one might first realize. Given that<br />

a type parameter is constrained to be a supertype of two distinct invocations of a generic<br />

type, say List and List, the naive intersection operation might yield<br />

Object. However, a more sophisticated analysis yields a set containing List. Similarly,<br />

if a type parameter, T, is constrained to be a supertype of two unrelated interfaces I<br />

and J, we might infer T must be Object, or we might obtain a tighter bound of I & J.<br />

<strong>The</strong>se issues are discussed in more detail later in this section.<br />

DRAFT<br />

We will use the following notational conventions in this section:<br />

• Type expressions are represented using the letters A, F, U, V and W. <strong>The</strong> letter<br />

A is only used to denote the type of an actual parameter, and F is only used to<br />

denote the type of a formal parameter.<br />

• Type parameters are represented using the letters S and T<br />

• Arguments to parameterized types are represented using the letters X, Y.<br />

• Generic type declarations are represented using the letters G and H.<br />

451

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!