opt
nimony/lib/std/opt.nim
opt — an optional value built on Nimony's native sum types.
The sum-type optional (Opt[T]), distinct from a Nim 2-compatible Option[T]. None is the absent (fieldless) case, Some carries a value. none[T]() constructs through result so the fieldless None takes its Opt[T] type from the typed result slot (a bare None() in expression position has nothing to infer T from).
unsafeGet raises BugError on None (a system ErrorCode — calling it without guarding is a programming error); guard with isSome, or use the total get(default).
import std/opt proc find(k: string): Opt[int] = if k == "x": result = some(1) else: result = none[int]() let r = find("x") if r.isSome: echo r.get(0)
type Opt = object case `kind: `sumtype of None: discard of Some: val: T
func some(v: T): Opt[T]template default(x: typedesc[Opt[T]]): Opt[T]None— makesOptfields work in enclosing objects'default/T()construction (the DefaultObj expansion resolvesdefaultper field, and sum types have no compiler-provided default). A template so it out-ranks the DefaultObj magic in overload resolution at construction sites.func none(): Opt[T]func isSome(o: Opt[T]): boolfunc isNone(o: Opt[T]): boolfunc get(o: Opt[T]; default: T): Tproc getOrQuit(o: Opt[T]): Tproc unsafeGet(o: Opt[T]): T