2021-03-05 21:07:48 +01:00
|
|
|
import std/options
|
|
|
|
|
|
|
|
|
|
include ./errorban
|
|
|
|
|
|
|
|
|
|
export options
|
|
|
|
|
|
|
|
|
|
template `?`*(T: typed): type Option[T] =
|
|
|
|
|
Option[T]
|
|
|
|
|
|
|
|
|
|
template `.?`*(option: ?typed, field: untyped{nkIdent}): ?untyped =
|
|
|
|
|
type T = type option.get.field
|
|
|
|
|
if option.isSome:
|
|
|
|
|
option.unsafeGet().field.some
|
|
|
|
|
else:
|
|
|
|
|
T.none
|
|
|
|
|
|
|
|
|
|
template `|?`*[T](option: ?T, fallback: T): T =
|
|
|
|
|
if option.isSome:
|
|
|
|
|
option.unsafeGet()
|
|
|
|
|
else:
|
|
|
|
|
fallback
|
|
|
|
|
|
|
|
|
|
template `=?`*[T](name: untyped{nkIdent}, option: ?T): bool =
|
|
|
|
|
template name: T {.used.} = option.unsafeGet()
|
|
|
|
|
option.isSome
|
|
|
|
|
|
2021-03-08 16:11:01 +01:00
|
|
|
template liftUnary(_: type Option, operator: untyped) =
|
2021-03-07 08:56:35 +01:00
|
|
|
|
|
|
|
|
template `operator`*(a: ?typed): ?typed =
|
2021-03-07 13:01:52 +01:00
|
|
|
type T {.used.} = type(`operator`(a.unsafeGet))
|
2021-03-07 08:56:35 +01:00
|
|
|
if x =? a:
|
|
|
|
|
`operator`(x).some
|
|
|
|
|
else:
|
|
|
|
|
T.none
|
|
|
|
|
|
2021-03-08 16:11:01 +01:00
|
|
|
template liftBinary(_: type Option, operator: untyped) =
|
2021-03-07 08:56:35 +01:00
|
|
|
|
|
|
|
|
template `operator`*(a: ?typed, b: ?typed): ?typed =
|
|
|
|
|
type T = type(`operator`(a.unsafeGet, b.unsafeGet))
|
|
|
|
|
if x =? a and y =? b:
|
|
|
|
|
`operator`(x, y).some
|
|
|
|
|
else:
|
|
|
|
|
T.none
|
|
|
|
|
|
|
|
|
|
template `operator`*(a: ?typed, b: typed): ?typed =
|
|
|
|
|
type T = type(`operator`(a.unsafeGet, b))
|
|
|
|
|
if x =? a:
|
|
|
|
|
`operator`(x, b).some
|
|
|
|
|
else:
|
|
|
|
|
T.none
|
|
|
|
|
|
2021-03-08 16:11:01 +01:00
|
|
|
Option.liftUnary(`-`)
|
|
|
|
|
Option.liftUnary(`+`)
|
|
|
|
|
Option.liftUnary(`@`)
|
|
|
|
|
Option.liftBinary(`[]`)
|
|
|
|
|
Option.liftBinary(`*`)
|
|
|
|
|
Option.liftBinary(`/`)
|
|
|
|
|
Option.liftBinary(`div`)
|
|
|
|
|
Option.liftBinary(`mod`)
|
|
|
|
|
Option.liftBinary(`shl`)
|
|
|
|
|
Option.liftBinary(`shr`)
|
|
|
|
|
Option.liftBinary(`+`)
|
|
|
|
|
Option.liftBinary(`-`)
|
|
|
|
|
Option.liftBinary(`&`)
|
|
|
|
|
Option.liftBinary(`<=`)
|
|
|
|
|
Option.liftBinary(`<`)
|
|
|
|
|
Option.liftBinary(`>=`)
|
|
|
|
|
Option.liftBinary(`>`)
|