Xo is experimental. The source is now public on GitHub, or try Xo in your browser.
Menu · XO0204

Docs

XO0204: `?` on an Option in a function returning a Result

error · reported by the checker

opt? returns None from a function that returns an optional. In a function that returns a Result there is no error value to return for None, so say which error a missing value is: opt.or_err(E.Missing)?. The diagnostic offers that fix. Directly in a test block (not in a helper function or a fn literal), opt? fails the test with missing value: opt instead (decision 0347). See core 2.5. Coming from Go: knowledgebase/guides/option-result-for-go.md maps if err != nil, v, ok := m[k], error wrapping, and errors.Is to Xo (https://github.com/xo-lang/xo/blob/main/knowledgebase/guides/option-result-for-go.md).

Example

enum E { Missing }

fn f(s: Str?) -> Str ! E { s? }

Fix

enum E { Missing }

fn f(s: Str?) -> Str ! E { s.or_err(E.Missing)? }

Run xo explain XO0204 for this text in a terminal, or see section 10.6 of the specification.