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

# 18.5.5. Record Pattern Type Inference

When a record pattern ([§14.30.1](14.30.1.md)) for a generic record class R appears in a context in which values of a type T will be matched against it, and the pattern does not provide type arguments for R, the type arguments are inferred, as described below.

1. If T is not checked cast convertible ([§5.5](5.5.md)) to the raw type R, inference fails.

2. Otherwise, where P_1, ..., P_n (*n* ≥ 1) are the type parameters of R, let α_1, ..., α_n be inference variables. An initial bound set, B_0, is generated from the declared bounds of P_1, ..., P_n, as described in [§18.1.3](18.1.3.md).

3. A type T' is derived from T, as follows:

   If T is a parameterized type, let T_c be the result of capture conversion ([§5.1.10](5.1.10.md)) applied to T, and let Z_1, ..., Z_k (*k* ≥ 0) be the type variables produced by capture that are type arguments in T_c. (This includes type variables produced by the capture conversion in this step, and type variables produced by capture conversion elsewhere.) Let β_1, ..., β_k (*k* ≥ 0) be inference variables, and let θ be the substitution `[`Z_1:=β_1, ..., Z_k:=β_k`]`. T' is T_c θ.

   Additional bounds for β_1, ..., β_k are incorporated into B_0 to form a bound set B_1, as follows:

   - If β_i (1 ≤ *i* ≤ *k*) replaced a type variable with an upper bound U, then the bound β_i `<:` U θ appears in the bound set.
   - If β_i (1 ≤ *i* ≤ *k*) replaced a type variable with a lower bound `L`, then the bound `L` θ `<:` β_i appears in the bound set.
   - If no proper upper bounds otherwise exist for β_i (1 ≤ *i* ≤ *k*), the bound β_i `<:` `Object` appears in the bound set.
   - If T is any other class or interface type, then T' is the same as T, and B_1 is the same as B_0.
   - If T is a type variable or an intersection type, then for each upper bound of the type variable or element of the intersection type, this step and step 4 are repeated recursively. All bounds produced in steps 3 and 4 are incorporated into a single bound set.

4. If T' is a parameterization of a generic class G, and there exists a supertype of R`<`α_1, ..., α_n`>` that is also a parameterization of G, let R' be that supertype. The constraint formula ‹T'=R'› is reduced ([§18.2](18.2.md)) and the resulting bounds are incorporated into B_1 to produce a new bound set, B_2.

   Otherwise, B_2 is the same as B_1.

   If B_2 contains the bound *false*, inference fails.

5. Otherwise, the inference variables α_1, ..., α_n are resolved in B_2 ([§18.4](18.4.md)). Unlike normal resolution, in this case resolution skips the step that attempts to produce an instantiation for an inference variable from its proper lower bounds or proper upper bounds; instead, any new instantiations are created by skipping directly to the step that introduces fresh type variables.

   If resolution fails, then inference fails.

6. Otherwise, let A_1, ..., A_n be the resolved instantiations for α_1, ..., α_n, and let Y_1, ..., Y_p (*p* ≥ 0) be any fresh type variables introduced by resolution.

   The type of the record pattern is the upward projection of R`<`A_1, ..., A_n`>` with respect to Y_1, ..., Y_p ([§4.10.5](4.10.5.md)).

**Example 18.5.5-1. Record Pattern Type Inference**

The following program infers a parameterization for a record pattern:

```
import java.util.function.UnaryOperator;

record Mapper<T>(T in, T out) implements UnaryOperator<T> {
    public T apply(T arg) {
        return in.equals(arg) ? out : null;
    }
}

class IllustrateRecordPatternTypeInference{
    void test(UnaryOperator<? extends CharSequence> op) {
        if (op instanceof Mapper(var in, var out)) {
            boolean shorter = out.length() < in.length();
        }
    }
}
```

In this case, R is the record class `Mapper`, and T is the type `UnaryOperator`<`?` `extends` `CharSequence`>. T is checked cast convertible to raw `Mapper`, so we'll infer an instantiation for α in `Mapper`<α>. T' is the type `UnaryOperator`<β>, where β has upper bound `CharSequence`.

`Mapper`<α> has the supertype `UnaryOperator`<α>, so we'll reduce the constraint formula ‹`UnaryOperator`<β>= `UnaryOperator`<α>›. This leads to the bound α=β. Incorporation further infers that α `<:` `CharSequence`.

Now we resolve α, yielding α = Y, a fresh type variable with upper bound `CharSequence`. Finally, we find the upward projection of `Mapper`\<Y> with respect to Y, inferring that the type of the record pattern is `Mapper`<`?` `extends` `CharSequence`>.

Once we know the type of the record pattern, we can find its component types, which are matched against the component patterns of the record pattern. Pattern variables `in` and `out` both have type `CharSequence`.
