mirror of
https://github.com/logos-storage/questionable.git
synced 2026-01-02 22:03:10 +00:00
Use operator lifting for the indexing operator
This commit is contained in:
parent
fe8acac2cd
commit
59548bcd74
@ -14,13 +14,6 @@ template `.?`*(option: ?typed, field: untyped{nkIdent}): ?untyped =
|
||||
else:
|
||||
T.none
|
||||
|
||||
template `[]`*(option: ?typed, index: typed): ?typed =
|
||||
type T = type option.get[index]
|
||||
if option.isSome:
|
||||
option.unsafeGet()[index].some
|
||||
else:
|
||||
T.none
|
||||
|
||||
template `|?`*[T](option: ?T, fallback: T): T =
|
||||
if option.isSome:
|
||||
option.unsafeGet()
|
||||
@ -31,7 +24,7 @@ template `=?`*[T](name: untyped{nkIdent}, option: ?T): bool =
|
||||
template name: T {.used.} = option.unsafeGet()
|
||||
option.isSome
|
||||
|
||||
template liftPrefix(_: type Option, operator: untyped) =
|
||||
template liftUnary(_: type Option, operator: untyped) =
|
||||
|
||||
template `operator`*(a: ?typed): ?typed =
|
||||
type T {.used.} = type(`operator`(a.unsafeGet))
|
||||
@ -40,7 +33,7 @@ template liftPrefix(_: type Option, operator: untyped) =
|
||||
else:
|
||||
T.none
|
||||
|
||||
template liftInfix(_: type Option, operator: untyped) =
|
||||
template liftBinary(_: type Option, operator: untyped) =
|
||||
|
||||
template `operator`*(a: ?typed, b: ?typed): ?typed =
|
||||
type T = type(`operator`(a.unsafeGet, b.unsafeGet))
|
||||
@ -56,19 +49,20 @@ template liftInfix(_: type Option, operator: untyped) =
|
||||
else:
|
||||
T.none
|
||||
|
||||
Option.liftPrefix(`-`)
|
||||
Option.liftPrefix(`+`)
|
||||
Option.liftPrefix(`@`)
|
||||
Option.liftInfix(`*`)
|
||||
Option.liftInfix(`/`)
|
||||
Option.liftInfix(`div`)
|
||||
Option.liftInfix(`mod`)
|
||||
Option.liftInfix(`shl`)
|
||||
Option.liftInfix(`shr`)
|
||||
Option.liftInfix(`+`)
|
||||
Option.liftInfix(`-`)
|
||||
Option.liftInfix(`&`)
|
||||
Option.liftInfix(`<=`)
|
||||
Option.liftInfix(`<`)
|
||||
Option.liftInfix(`>=`)
|
||||
Option.liftInfix(`>`)
|
||||
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(`>`)
|
||||
|
||||
@ -20,13 +20,6 @@ template `.?`*(value: ?!typed, field: untyped{nkIdent}): ?!untyped =
|
||||
else:
|
||||
err(?!T, error(value))
|
||||
|
||||
template `[]`*(value: ?!typed, index: typed): ?!typed =
|
||||
type T = type value.get[index]
|
||||
if value.isOk:
|
||||
ok(?!T, value.unsafeGet()[index])
|
||||
else:
|
||||
err(?!T, error(value))
|
||||
|
||||
template `|?`*[T](value: ?!T, fallback: T): T =
|
||||
value.valueOr(fallback)
|
||||
|
||||
@ -34,7 +27,7 @@ template `=?`*[T](name: untyped{nkIdent}, value: ?!T): bool =
|
||||
template name: T {.used.} = value.unsafeGet()
|
||||
value.isOk
|
||||
|
||||
template liftPrefix(_: type Result, operator: untyped) =
|
||||
template liftUnary(_: type Result, operator: untyped) =
|
||||
|
||||
template `operator`*(a: ?!typed): ?!typed =
|
||||
type T {.used.} = type(`operator`(a.unsafeGet))
|
||||
@ -43,7 +36,7 @@ template liftPrefix(_: type Result, operator: untyped) =
|
||||
else:
|
||||
T.failure(a.error)
|
||||
|
||||
template liftInfix(_: type Result, operator: untyped) =
|
||||
template liftBinary(_: type Result, operator: untyped) =
|
||||
|
||||
template `operator`*(a: ?!typed, b: ?!typed): ?!typed =
|
||||
type T = type(`operator`(a.unsafeGet, b.unsafeGet))
|
||||
@ -61,19 +54,20 @@ template liftInfix(_: type Result, operator: untyped) =
|
||||
else:
|
||||
T.failure(a.error)
|
||||
|
||||
Result.liftPrefix(`-`)
|
||||
Result.liftPrefix(`+`)
|
||||
Result.liftPrefix(`@`)
|
||||
Result.liftInfix(`*`)
|
||||
Result.liftInfix(`/`)
|
||||
Result.liftInfix(`div`)
|
||||
Result.liftInfix(`mod`)
|
||||
Result.liftInfix(`shl`)
|
||||
Result.liftInfix(`shr`)
|
||||
Result.liftInfix(`+`)
|
||||
Result.liftInfix(`-`)
|
||||
Result.liftInfix(`&`)
|
||||
Result.liftInfix(`<=`)
|
||||
Result.liftInfix(`<`)
|
||||
Result.liftInfix(`>=`)
|
||||
Result.liftInfix(`>`)
|
||||
Result.liftUnary(`-`)
|
||||
Result.liftUnary(`+`)
|
||||
Result.liftUnary(`@`)
|
||||
Result.liftBinary(`[]`)
|
||||
Result.liftBinary(`*`)
|
||||
Result.liftBinary(`/`)
|
||||
Result.liftBinary(`div`)
|
||||
Result.liftBinary(`mod`)
|
||||
Result.liftBinary(`shl`)
|
||||
Result.liftBinary(`shr`)
|
||||
Result.liftBinary(`+`)
|
||||
Result.liftBinary(`-`)
|
||||
Result.liftBinary(`&`)
|
||||
Result.liftBinary(`<=`)
|
||||
Result.liftBinary(`<`)
|
||||
Result.liftBinary(`>=`)
|
||||
Result.liftBinary(`>`)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user