---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 23 Edition"
java_version: 23
chapter: "Chapter 18. Type Inference"
section: "18.5.3"
heading: "18.5.3. Functional Interface Parameterization Inference"
parent: "18.5. Uses of Inference"
source: https://docs.oracle.com/javase/specs/jls/se23/html/jls-18.html#jls-18.5.3
id: "18.5.3"
url: https://jvm.obfuscated.software/jls/se23/18.5.3.html
raw: https://jvm.obfuscated.software/jls/se23/18.5.3.md
---

# 18.5.3. Functional Interface Parameterization Inference

Where a lambda expression with explicit parameter types P_1, ..., P_n targets a functional interface type F`<`A_1, ..., A_m`>` with at least one wildcard type argument, then a parameterization of F may be derived as the ground target type of the lambda expression as follows.

Let Q_1, ..., Q_k be the parameter types of the function type of the type F`<`α_1, ..., α_m`>`, where α_1, ..., α_m are fresh inference variables.

If *n* ≠ *k*, no valid parameterization exists. Otherwise, a set of constraint formulas is formed with, for all *i* (1 ≤ *i* ≤ *n*), ‹P_i = Q_i›. This constraint formula set is reduced to form the bound set B.

If B contains the bound *false*, no valid parameterization exists. Otherwise, a new parameterization of the functional interface type, F`<`A'_1, ..., A'_m`>`, is constructed as follows, for 1 ≤ *i* ≤ *m*:

- If B contains an instantiation ([§18.1.3](18.1.3.md)) for α_i, T, then A'_i = T.
- Otherwise, A'_i = A_i.

If F`<`A'_1, ..., A'_m`>` is not a well-formed type (that is, the type arguments are not within their bounds), or if F`<`A'_1, ..., A'_m`>` is not a subtype of F`<`A_1, ..., A_m`>`, no valid parameterization exists. Otherwise, the inferred parameterization is either F`<`A'_1, ..., A'_m`>`, if all the type arguments are types, or the non-wildcard parameterization ([§9.9](9.9.md)) of F`<`A'_1, ..., A'_m`>`, if one or more type arguments are still wildcards.

In order to determine the function type of a wildcard-parameterized functional interface, we have to "instantiate" the wildcard type arguments with specific types. The "default" approach is to simply replace the wildcards with their bounds, as described in [§9.8](9.8.md), but this produces spurious errors in cases where a lambda expression has explicit parameter types that do *not* correspond to the wildcard bounds. For example:

```
Predicate<? super Integer> p = (Number n) -> n.equals(23);
```

The lambda expression is a `Predicate<Number>`, which is a subtype of `Predicate<? super Integer>` but not `Predicate<Integer>`. The analysis in this section is used to infer that `Number` is an appropriate choice for the type argument to `Predicate`.

That said, the analysis here, while described in terms of general type inference, is intentionally quite simple. The only constraints are equality constraints, which means that reduction amounts to simple pattern matching. A more powerful strategy might also infer constraints from the body of the lambda expression. But, given possible interactions with inference for surrounding and/or nested generic method invocations, this would introduce a lot of extra complexity.
