Simpler unpack for option

This commit is contained in:
Tanguy 2022-06-20 15:17:32 +02:00
parent ab4d65ac83
commit 1e8ac329f8
No known key found for this signature in database
GPG Key ID: 7DD8EC6B6CE6C45E

View File

@ -1,16 +1,12 @@
import std/options
import std/macros
proc placeholder(T: type): T =
discard
template questionableUnpack*[T](expression: Option[T]): (T, bool) =
proc questionableUnpack[T](option: Option[T]): (T, bool) =
## Used internally
let option = expression
type T = typeof(option.unsafeGet())
let res = if option.isSome: option.unsafeGet() else: placeholder(T)
(res, option.isSome)
if option.isSome:
return (option.unsafeGet(), true)
# return default
macro `=?`*(name, expression): bool =
## The `=?` operator lets you bind the value inside an Option or Result to a