Saltar al contenido
TE126 · Tipos · error

No se puede inferir el struct detrás de `let { … }`

Un patrón de struct anónimo necesita saber qué struct desestructura, pero el tipo del valor no resuelve a un struct concreto — normalmente por falta de anotación de tipo.

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.

Véase también

Buscar en Zolo

9 resultados

en