mirror of
https://github.com/status-im/nim-stew.git
synced 2025-01-09 11:45:42 +00:00
result: add Opt.some
/ Opt.none
(#117)
This commit is contained in:
parent
981551ae5d
commit
dad28a269f
@ -884,6 +884,24 @@ func filter*[T](
|
|||||||
|
|
||||||
# Options compatibility
|
# Options compatibility
|
||||||
|
|
||||||
|
template some*[T](O: type Opt, v: T): Opt[T] =
|
||||||
|
## Create an `Opt` set to a value
|
||||||
|
##
|
||||||
|
## ```
|
||||||
|
## let o = Opt.some(42)
|
||||||
|
## assert o.isSome and o.get() == 42
|
||||||
|
## ```
|
||||||
|
Opt[T].ok(v)
|
||||||
|
|
||||||
|
template none*(O: type Opt, T: type): Opt[T] =
|
||||||
|
## Create an `Opt` set to none
|
||||||
|
##
|
||||||
|
## ```
|
||||||
|
## let o = Opt.none(int)
|
||||||
|
## assert o.isNone
|
||||||
|
## ```
|
||||||
|
Opt[T].err()
|
||||||
|
|
||||||
template isSome*(o: Opt): bool =
|
template isSome*(o: Opt): bool =
|
||||||
## Alias for `isOk`
|
## Alias for `isOk`
|
||||||
isOk o
|
isOk o
|
||||||
|
@ -351,6 +351,9 @@ block: # Result[T, void] aka `Opt`
|
|||||||
doAssert oOk.filter(proc(x: int): bool = false).isErr()
|
doAssert oOk.filter(proc(x: int): bool = false).isErr()
|
||||||
doAssert oErr.filter(proc(x: int): bool = true) == oErr
|
doAssert oErr.filter(proc(x: int): bool = true) == oErr
|
||||||
|
|
||||||
|
doAssert Opt.some(42).get() == 42
|
||||||
|
doAssert Opt.none(int).isNone()
|
||||||
|
|
||||||
block: # `cstring` dangling reference protection
|
block: # `cstring` dangling reference protection
|
||||||
type CSRes = Result[void, cstring]
|
type CSRes = Result[void, cstring]
|
||||||
|
|
||||||
@ -398,4 +401,3 @@ block: # Experiments
|
|||||||
counter2 += 1
|
counter2 += 1
|
||||||
|
|
||||||
doAssert counter2 == 1, "one-item collection when set"
|
doAssert counter2 == 1, "one-item collection when set"
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user