The whole options module should be inline (#14417) [backport:1.2]
* The whole options module should be inline * Use inline per proc and tag `lent` where appropriate * Remove lent annotation (failing at compiletime)
This commit is contained in:
parent
5caaa4bf6f
commit
f71f8b0239
2 changed files with 14 additions and 15 deletions
BIN
build.out
Executable file
BIN
build.out
Executable file
Binary file not shown.
|
|
@ -77,7 +77,7 @@ type
|
||||||
UnpackDefect* = object of Defect
|
UnpackDefect* = object of Defect
|
||||||
UnpackError* {.deprecated: "See corresponding Defect".} = UnpackDefect
|
UnpackError* {.deprecated: "See corresponding Defect".} = UnpackDefect
|
||||||
|
|
||||||
proc option*[T](val: T): Option[T] =
|
proc option*[T](val: T): Option[T] {.inline.} =
|
||||||
## Can be used to convert a pointer type (`ptr` or `ref` or `proc`) to an option type.
|
## Can be used to convert a pointer type (`ptr` or `ref` or `proc`) to an option type.
|
||||||
## It converts `nil` to `None`.
|
## It converts `nil` to `None`.
|
||||||
##
|
##
|
||||||
|
|
@ -98,7 +98,7 @@ proc option*[T](val: T): Option[T] =
|
||||||
when T isnot SomePointer:
|
when T isnot SomePointer:
|
||||||
result.has = true
|
result.has = true
|
||||||
|
|
||||||
proc some*[T](val: T): Option[T] =
|
proc some*[T](val: T): Option[T] {.inline.} =
|
||||||
## Returns an `Option` that has the value `val`.
|
## Returns an `Option` that has the value `val`.
|
||||||
##
|
##
|
||||||
## See also:
|
## See also:
|
||||||
|
|
@ -121,7 +121,7 @@ proc some*[T](val: T): Option[T] =
|
||||||
result.has = true
|
result.has = true
|
||||||
result.val = val
|
result.val = val
|
||||||
|
|
||||||
proc none*(T: typedesc): Option[T] =
|
proc none*(T: typedesc): Option[T] {.inline.} =
|
||||||
## Returns an `Option` for this type that has no value.
|
## Returns an `Option` for this type that has no value.
|
||||||
##
|
##
|
||||||
## See also:
|
## See also:
|
||||||
|
|
@ -136,7 +136,7 @@ proc none*(T: typedesc): Option[T] =
|
||||||
# the default is the none type
|
# the default is the none type
|
||||||
discard
|
discard
|
||||||
|
|
||||||
proc none*[T]: Option[T] =
|
proc none*[T]: Option[T] {.inline.} =
|
||||||
## Alias for `none(T) proc <#none,typedesc>`_.
|
## Alias for `none(T) proc <#none,typedesc>`_.
|
||||||
none(T)
|
none(T)
|
||||||
|
|
||||||
|
|
@ -167,7 +167,7 @@ proc isNone*[T](self: Option[T]): bool {.inline.} =
|
||||||
else:
|
else:
|
||||||
not self.has
|
not self.has
|
||||||
|
|
||||||
proc get*[T](self: Option[T]): T =
|
proc get*[T](self: Option[T]): T {.inline.} =
|
||||||
## Returns contents of an `Option`. If it is `None`, then an exception is
|
## Returns contents of an `Option`. If it is `None`, then an exception is
|
||||||
## thrown.
|
## thrown.
|
||||||
##
|
##
|
||||||
|
|
@ -185,7 +185,7 @@ proc get*[T](self: Option[T]): T =
|
||||||
raise newException(UnpackError, "Can't obtain a value from a `none`")
|
raise newException(UnpackError, "Can't obtain a value from a `none`")
|
||||||
self.val
|
self.val
|
||||||
|
|
||||||
proc get*[T](self: Option[T], otherwise: T): T =
|
proc get*[T](self: Option[T], otherwise: T): T {.inline.} =
|
||||||
## Returns the contents of the `Option` or an `otherwise` value if
|
## Returns the contents of the `Option` or an `otherwise` value if
|
||||||
## the `Option` is `None`.
|
## the `Option` is `None`.
|
||||||
runnableExamples:
|
runnableExamples:
|
||||||
|
|
@ -200,7 +200,7 @@ proc get*[T](self: Option[T], otherwise: T): T =
|
||||||
else:
|
else:
|
||||||
otherwise
|
otherwise
|
||||||
|
|
||||||
proc get*[T](self: var Option[T]): var T =
|
proc get*[T](self: var Option[T]): var T {.inline.} =
|
||||||
## Returns contents of the `var Option`. If it is `None`, then an exception
|
## Returns contents of the `var Option`. If it is `None`, then an exception
|
||||||
## is thrown.
|
## is thrown.
|
||||||
runnableExamples:
|
runnableExamples:
|
||||||
|
|
@ -215,7 +215,7 @@ proc get*[T](self: var Option[T]): var T =
|
||||||
raise newException(UnpackError, "Can't obtain a value from a `none`")
|
raise newException(UnpackError, "Can't obtain a value from a `none`")
|
||||||
return self.val
|
return self.val
|
||||||
|
|
||||||
proc map*[T](self: Option[T], callback: proc (input: T)) =
|
proc map*[T](self: Option[T], callback: proc (input: T)) {.inline.} =
|
||||||
## 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:
|
||||||
|
|
@ -239,7 +239,7 @@ proc map*[T](self: Option[T], callback: proc (input: T)) =
|
||||||
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] =
|
proc map*[T, R](self: Option[T], callback: proc (input: T): R): Option[R] {.inline.} =
|
||||||
## 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.
|
||||||
##
|
##
|
||||||
|
|
@ -266,7 +266,7 @@ proc map*[T, R](self: Option[T], callback: proc (input: T): R): Option[R] =
|
||||||
else:
|
else:
|
||||||
none(R)
|
none(R)
|
||||||
|
|
||||||
proc flatten*[A](self: Option[Option[A]]): Option[A] =
|
proc flatten*[A](self: Option[Option[A]]): Option[A] {.inline.} =
|
||||||
## Remove one level of structure in a nested `Option`.
|
## Remove one level of structure in a nested `Option`.
|
||||||
runnableExamples:
|
runnableExamples:
|
||||||
let a = some(some(42))
|
let a = some(some(42))
|
||||||
|
|
@ -278,7 +278,7 @@ proc flatten*[A](self: Option[Option[A]]): Option[A] =
|
||||||
none(A)
|
none(A)
|
||||||
|
|
||||||
proc flatMap*[A, B](self: Option[A],
|
proc flatMap*[A, B](self: Option[A],
|
||||||
callback: proc (input: A): Option[B]): Option[B] =
|
callback: proc (input: A): Option[B]): Option[B] {.inline.} =
|
||||||
## 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.
|
||||||
##
|
##
|
||||||
|
|
@ -308,7 +308,7 @@ proc flatMap*[A, B](self: Option[A],
|
||||||
|
|
||||||
map(self, callback).flatten()
|
map(self, callback).flatten()
|
||||||
|
|
||||||
proc filter*[T](self: Option[T], callback: proc (input: T): bool): Option[T] =
|
proc filter*[T](self: Option[T], callback: proc (input: T): bool): Option[T] {.inline.} =
|
||||||
## 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`.
|
||||||
|
|
@ -333,7 +333,7 @@ proc filter*[T](self: Option[T], callback: proc (input: T): bool): Option[T] =
|
||||||
else:
|
else:
|
||||||
self
|
self
|
||||||
|
|
||||||
proc `==`*(a, b: Option): bool =
|
proc `==`*(a, b: Option): bool {.inline.} =
|
||||||
## Returns `true` if both `Option`s are `None`,
|
## Returns `true` if both `Option`s are `None`,
|
||||||
## or if they are both `Some` and have equal values.
|
## or if they are both `Some` and have equal values.
|
||||||
runnableExamples:
|
runnableExamples:
|
||||||
|
|
@ -363,7 +363,7 @@ proc `$`*[T](self: Option[T]): string =
|
||||||
else:
|
else:
|
||||||
result = "None[" & name(T) & "]"
|
result = "None[" & name(T) & "]"
|
||||||
|
|
||||||
proc unsafeGet*[T](self: Option[T]): T =
|
proc unsafeGet*[T](self: Option[T]): T {.inline.}=
|
||||||
## Returns the value of a `some`. Behavior is undefined for `none`.
|
## Returns the value of a `some`. Behavior is undefined for `none`.
|
||||||
##
|
##
|
||||||
## **Note:** Use it only when you are **absolutely sure** the value is present
|
## **Note:** Use it only when you are **absolutely sure** the value is present
|
||||||
|
|
@ -513,4 +513,3 @@ when isMainModule:
|
||||||
test "Ref type with overloaded `==`":
|
test "Ref type with overloaded `==`":
|
||||||
let p = some(RefPerson.new())
|
let p = some(RefPerson.new())
|
||||||
check p.isSome
|
check p.isSome
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue