Use operator lifting for the indexing operator

This commit is contained in:
Mark Spanbroek 2021-03-08 16:11:01 +01:00
parent fe8acac2cd
commit 59548bcd74
2 changed files with 38 additions and 50 deletions

View File

@ -14,13 +14,6 @@ template `.?`*(option: ?typed, field: untyped{nkIdent}): ?untyped =
else: else:
T.none 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 = template `|?`*[T](option: ?T, fallback: T): T =
if option.isSome: if option.isSome:
option.unsafeGet() option.unsafeGet()
@ -31,7 +24,7 @@ template `=?`*[T](name: untyped{nkIdent}, option: ?T): bool =
template name: T {.used.} = option.unsafeGet() template name: T {.used.} = option.unsafeGet()
option.isSome option.isSome
template liftPrefix(_: type Option, operator: untyped) = template liftUnary(_: type Option, operator: untyped) =
template `operator`*(a: ?typed): ?typed = template `operator`*(a: ?typed): ?typed =
type T {.used.} = type(`operator`(a.unsafeGet)) type T {.used.} = type(`operator`(a.unsafeGet))
@ -40,7 +33,7 @@ template liftPrefix(_: type Option, operator: untyped) =
else: else:
T.none T.none
template liftInfix(_: type Option, operator: untyped) = template liftBinary(_: type Option, operator: untyped) =
template `operator`*(a: ?typed, b: ?typed): ?typed = template `operator`*(a: ?typed, b: ?typed): ?typed =
type T = type(`operator`(a.unsafeGet, b.unsafeGet)) type T = type(`operator`(a.unsafeGet, b.unsafeGet))
@ -56,19 +49,20 @@ template liftInfix(_: type Option, operator: untyped) =
else: else:
T.none T.none
Option.liftPrefix(`-`) Option.liftUnary(`-`)
Option.liftPrefix(`+`) Option.liftUnary(`+`)
Option.liftPrefix(`@`) Option.liftUnary(`@`)
Option.liftInfix(`*`) Option.liftBinary(`[]`)
Option.liftInfix(`/`) Option.liftBinary(`*`)
Option.liftInfix(`div`) Option.liftBinary(`/`)
Option.liftInfix(`mod`) Option.liftBinary(`div`)
Option.liftInfix(`shl`) Option.liftBinary(`mod`)
Option.liftInfix(`shr`) Option.liftBinary(`shl`)
Option.liftInfix(`+`) Option.liftBinary(`shr`)
Option.liftInfix(`-`) Option.liftBinary(`+`)
Option.liftInfix(`&`) Option.liftBinary(`-`)
Option.liftInfix(`<=`) Option.liftBinary(`&`)
Option.liftInfix(`<`) Option.liftBinary(`<=`)
Option.liftInfix(`>=`) Option.liftBinary(`<`)
Option.liftInfix(`>`) Option.liftBinary(`>=`)
Option.liftBinary(`>`)

View File

@ -20,13 +20,6 @@ template `.?`*(value: ?!typed, field: untyped{nkIdent}): ?!untyped =
else: else:
err(?!T, error(value)) 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 = template `|?`*[T](value: ?!T, fallback: T): T =
value.valueOr(fallback) value.valueOr(fallback)
@ -34,7 +27,7 @@ template `=?`*[T](name: untyped{nkIdent}, value: ?!T): bool =
template name: T {.used.} = value.unsafeGet() template name: T {.used.} = value.unsafeGet()
value.isOk value.isOk
template liftPrefix(_: type Result, operator: untyped) = template liftUnary(_: type Result, operator: untyped) =
template `operator`*(a: ?!typed): ?!typed = template `operator`*(a: ?!typed): ?!typed =
type T {.used.} = type(`operator`(a.unsafeGet)) type T {.used.} = type(`operator`(a.unsafeGet))
@ -43,7 +36,7 @@ template liftPrefix(_: type Result, operator: untyped) =
else: else:
T.failure(a.error) T.failure(a.error)
template liftInfix(_: type Result, operator: untyped) = template liftBinary(_: type Result, operator: untyped) =
template `operator`*(a: ?!typed, b: ?!typed): ?!typed = template `operator`*(a: ?!typed, b: ?!typed): ?!typed =
type T = type(`operator`(a.unsafeGet, b.unsafeGet)) type T = type(`operator`(a.unsafeGet, b.unsafeGet))
@ -61,19 +54,20 @@ template liftInfix(_: type Result, operator: untyped) =
else: else:
T.failure(a.error) T.failure(a.error)
Result.liftPrefix(`-`) Result.liftUnary(`-`)
Result.liftPrefix(`+`) Result.liftUnary(`+`)
Result.liftPrefix(`@`) Result.liftUnary(`@`)
Result.liftInfix(`*`) Result.liftBinary(`[]`)
Result.liftInfix(`/`) Result.liftBinary(`*`)
Result.liftInfix(`div`) Result.liftBinary(`/`)
Result.liftInfix(`mod`) Result.liftBinary(`div`)
Result.liftInfix(`shl`) Result.liftBinary(`mod`)
Result.liftInfix(`shr`) Result.liftBinary(`shl`)
Result.liftInfix(`+`) Result.liftBinary(`shr`)
Result.liftInfix(`-`) Result.liftBinary(`+`)
Result.liftInfix(`&`) Result.liftBinary(`-`)
Result.liftInfix(`<=`) Result.liftBinary(`&`)
Result.liftInfix(`<`) Result.liftBinary(`<=`)
Result.liftInfix(`>=`) Result.liftBinary(`<`)
Result.liftInfix(`>`) Result.liftBinary(`>=`)
Result.liftBinary(`>`)