std/options enables stricteffects (#19441)

This commit is contained in:
ringabout 2022-08-25 15:21:46 +08:00 • committed by GitHub
commit 16f6dc05fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -68,6 +68,10 @@ supports pattern matching on `Option`s, with the `Some(<pattern>)` and
]## ]##
# xxx pending https://github.com/timotheecour/Nim/issues/376 use `runnableExamples` and `whichModule` # xxx pending https://github.com/timotheecour/Nim/issues/376 use `runnableExamples` and `whichModule`
when defined(nimHasEffectsOf):
{.experimental: "strictEffects".}
else:
{.pragma: effectsOf.}
import typetraits import typetraits
@ -226,7 +230,7 @@ proc get*[T](self: var Option[T]): var T {.inline.} =
raise newException(UnpackDefect, "Can't obtain a value from a `none`") raise newException(UnpackDefect, "Can't obtain a value from a `none`")
return self.val return self.val
proc map*[T](self: Option[T], callback: proc (input: T)) {.inline.} = proc map*[T](self: Option[T], callback: proc (input: T)) {.inline, effectsOf: callback.} =
## Applies a `callback` function to the value of the `Option`, if it has one. ## Applies a `callback` function to the value of the `Option`, if it has one.
## ##
## **See also:** ## **See also:**
@ -245,7 +249,7 @@ proc map*[T](self: Option[T], callback: proc (input: T)) {.inline.} =
if self.isSome: if self.isSome:
callback(self.val) callback(self.val)
proc map*[T, R](self: Option[T], callback: proc (input: T): R): Option[R] {.inline.} = proc map*[T, R](self: Option[T], callback: proc (input: T): R): Option[R] {.inline, effectsOf: callback.} =
## Applies a `callback` function to the value of the `Option` and returns an ## Applies a `callback` function to the value of the `Option` and returns an
## `Option` containing the new value. ## `Option` containing the new value.
## ##
@ -282,7 +286,7 @@ proc flatten*[T](self: Option[Option[T]]): Option[T] {.inline.} =
none(T) none(T)
proc flatMap*[T, R](self: Option[T], proc flatMap*[T, R](self: Option[T],
callback: proc (input: T): Option[R]): Option[R] {.inline.} = callback: proc (input: T): Option[R]): Option[R] {.inline, effectsOf: callback.} =
## Applies a `callback` function to the value of the `Option` and returns the new value. ## Applies a `callback` function to the value of the `Option` and returns the new value.
## ##
## If the `Option` has no value, `none(R)` will be returned. ## If the `Option` has no value, `none(R)` will be returned.
@ -307,7 +311,7 @@ proc flatMap*[T, R](self: Option[T],
map(self, callback).flatten() map(self, callback).flatten()
proc filter*[T](self: Option[T], callback: proc (input: T): bool): Option[T] {.inline.} = proc filter*[T](self: Option[T], callback: proc (input: T): bool): Option[T] {.inline, effectsOf: callback.} =
## Applies a `callback` to the value of the `Option`. ## Applies a `callback` to the value of the `Option`.
## ##
## If the `callback` returns `true`, the option is returned as `some`. ## If the `callback` returns `true`, the option is returned as `some`.