Created
August 18, 2017 11:44
-
-
Save zacharycarter/844b9898e06def6d18e00e3d209aaf9d to your computer and use it in GitHub Desktop.
Snippet from https://play.nim-lang.org
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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