Skip to content

Instantly share code, notes, and snippets.

@zacharycarter
Created August 18, 2017 11:44
Show Gist options
  • Save zacharycarter/844b9898e06def6d18e00e3d209aaf9d to your computer and use it in GitHub Desktop.
Save zacharycarter/844b9898e06def6d18e00e3d209aaf9d to your computer and use it in GitHub Desktop.
import options
import typetraits
template hasValue[T](o: Option[T]): bool =
proc default(): T = discard
let isSome = o.isSome()
(let `o` {.inject.} = (if isSome: o.get() else: default()); isSome)
let x = some(123)
echo(type(x).name) # Option[system.int]
echo(x) # some(123)
if x.hasValue():
echo(type(x).name) # int
echo(x) # 123
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment