fix the wrong examples (#17035)
This commit is contained in:
parent
4886f8a02d
commit
c8d9963150
1 changed files with 42 additions and 19 deletions
|
|
@ -584,14 +584,28 @@ template withValue*[A, B](t: var Table[A, B], key: A, value, body: untyped) =
|
||||||
## Retrieves the value at `t[key]`.
|
## Retrieves the value at `t[key]`.
|
||||||
##
|
##
|
||||||
## `value` can be modified in the scope of the `withValue` call.
|
## `value` can be modified in the scope of the `withValue` call.
|
||||||
##
|
runnableExamples:
|
||||||
## .. code-block:: nim
|
type
|
||||||
##
|
User = object
|
||||||
## sharedTable.withValue(key, value) do:
|
name: string
|
||||||
## # block is executed only if `key` in `t`
|
uid: int
|
||||||
## value.name = "username"
|
|
||||||
## value.uid = 1000
|
var t = initTable[int, User]()
|
||||||
##
|
let u = User(name: "Hello", uid: 99)
|
||||||
|
t[1] = u
|
||||||
|
|
||||||
|
t.withValue(1, value) do:
|
||||||
|
# block is executed only if `key` in `t`
|
||||||
|
value.name = "Nim"
|
||||||
|
value.uid = 1314
|
||||||
|
|
||||||
|
t.withValue(2, value) do:
|
||||||
|
value.name = "No"
|
||||||
|
value.uid = 521
|
||||||
|
|
||||||
|
doAssert t[1].name == "Nim"
|
||||||
|
doAssert t[1].uid == 1314
|
||||||
|
|
||||||
mixin rawGet
|
mixin rawGet
|
||||||
var hc: Hash
|
var hc: Hash
|
||||||
var index = rawGet(t, key, hc)
|
var index = rawGet(t, key, hc)
|
||||||
|
|
@ -605,17 +619,26 @@ template withValue*[A, B](t: var Table[A, B], key: A,
|
||||||
## Retrieves the value at `t[key]`.
|
## Retrieves the value at `t[key]`.
|
||||||
##
|
##
|
||||||
## `value` can be modified in the scope of the `withValue` call.
|
## `value` can be modified in the scope of the `withValue` call.
|
||||||
##
|
runnableExamples:
|
||||||
## .. code-block:: nim
|
type
|
||||||
##
|
User = object
|
||||||
## table.withValue(key, value) do:
|
name: string
|
||||||
## # block is executed only if `key` in `t`
|
uid: int
|
||||||
## value.name = "username"
|
|
||||||
## value.uid = 1000
|
var t = initTable[int, User]()
|
||||||
## do:
|
let u = User(name: "Hello", uid: 99)
|
||||||
## # block is executed when `key` not in `t`
|
t[1] = u
|
||||||
## raise newException(KeyError, "Key not found")
|
|
||||||
##
|
t.withValue(1, value) do:
|
||||||
|
# block is executed only if `key` in `t`
|
||||||
|
value.name = "Nim"
|
||||||
|
value.uid = 1314
|
||||||
|
# do:
|
||||||
|
# # block is executed when `key` not in `t`
|
||||||
|
# raise newException(KeyError, "Key not found")
|
||||||
|
|
||||||
|
doAssert t[1].name == "Nim"
|
||||||
|
doAssert t[1].uid == 1314
|
||||||
mixin rawGet
|
mixin rawGet
|
||||||
var hc: Hash
|
var hc: Hash
|
||||||
var index = rawGet(t, key, hc)
|
var index = rawGet(t, key, hc)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue