Skip to content
TE126 · Type · error

Cannot infer the struct behind `let { … }`

An anonymous struct pattern needs to know which struct it destructures, but the value's type doesn't resolve to a concrete struct — usually because it has no type annotation.

Why this fires

An anonymous struct pattern (let { x, y } = v, or a bare { x } in parameter position) needs to know which struct it's destructuring, but the value's type can't be resolved to a concrete struct — usually because the value (or parameter) has no type annotation.

fn f(v) {
    let { x } = v
    //  ^^^^^ error[TE126]: cannot infer the struct behind `let { ... }` —
    //        annotate the value's type or name the struct
    print(x)
}

The same applies to anonymous parameter patterns:

fn greet({ name }) { print(name) }
//        ^^^^^^^ error[TE126]: anonymous pattern parameter needs a type —
//                 annotate `{ .. }: Type` or name the struct

Lambda parameters are exempt (|{ name }| name stays gradual) since lambdas are typically used generically over whatever shape is passed in.

Fix it

1. Annotate the value's type

struct User { name: str, age: int }

fn f(u: User) {
    let { name, .. } = u        // ok — type known from the parameter annotation
    print(name)
}

2. Name the struct explicitly

struct User { name: str, age: int }

fn f(v) {
    let User { name, .. } = v    // ok — struct named explicitly
    print(name)
}

See also

  • TE127 — a named pattern's struct disagrees with its type annotation.
  • TE123 — unknown field in a destructuring pattern.
  • /docs/pattern-matching — anonymous struct patterns.

See also

Search Zolo

9 results

en