Pular para o conteúdo
TE109 · Tipos · error

Não é possível inferir o tipo do campo pelo padrão

Um campo de struct foi escrito sem anotação de tipo, contando com o valor padrão para inferi-lo (`nome = valor`) — mas o tipo do padrão é ambíguo (ex.: `nil`, ou uma expressão que resolve para `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 no Zolo

9 resultados

en