Saltar al contenido
TE109 · Tipos · error

No se puede inferir el tipo del campo por su valor por defecto

Un campo de struct se escribió sin anotación de tipo, apoyándose en su valor por defecto para inferirlo (`nombre = valor`) — pero el tipo de ese valor es ambiguo (p. ej. `nil`, o una expresión que resuelve a `any`).

Why this fires

A struct field is written without a type annotation, relying on its default value to infer the type (name = value) — but the default's type can't be determined unambiguously (e.g. the default is nil, or another expression whose type resolves to any/unknown).

struct Config {
    retries = 3,
    timeout = nil,
    //        ^^^ error[TE109]: cannot infer the type of field 'timeout' from its
    //            default (annotate it explicitly, e.g. `timeout: <type> = ...`)
}

Fix it

Annotate the field's type explicitly:

struct Config {
    retries = 3,             // ok — inferred as int
    timeout: int? = nil,     // ok — explicit optional type
}

See also

Buscar en Zolo

9 resultados

en