summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorflywind <43030857+xflywind@users.noreply.github.com>2021-02-15 03:44:46 -0600
committerGitHub <noreply@github.com>2021-02-15 10:44:46 +0100
commitc8d99631503b03266db14967495c0a1b250ab327 (patch)
tree0383500b725de247ff2608976152cb76c076df07 /lib
parent4886f8a02d8e9fe24d20202e9e0ea365db99c797 (diff)
downloadNim-c8d99631503b03266db14967495c0a1b250ab327.tar.gz
fix the wrong examples (#17035)
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/collections/tables.nim61
1 files changed, 42 insertions, 19 deletions
diff --git a/lib/pure/collections/tables.nim b/lib/pure/collections/tables.nim
index 6a605fe62..74f658261 100644
--- a/lib/pure/collections/tables.nim
+++ b/lib/pure/collections/tables.nim
@@ -584,14 +584,28 @@ template withValue*[A, B](t: var Table[A, B], key: A, value, body: untyped) =
   ## Retrieves the value at `t[key]`.
   ##
   ## `value` can be modified in the scope of the `withValue` call.
-  ##
-  ## .. code-block:: nim
-  ##
-  ##   sharedTable.withValue(key, value) do:
-  ##     # block is executed only if `key` in `t`
-  ##     value.name = "username"
-  ##     value.uid = 1000
-  ##
+  runnableExamples:
+    type
+      User = object
+        name: string
+        uid: int
+
+    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
   var hc: Hash
   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]`.
   ##
   ## `value` can be modified in the scope of the `withValue` call.
-  ##
-  ## .. code-block:: nim
-  ##
-  ##   table.withValue(key, value) do:
-  ##     # block is executed only if `key` in `t`
-  ##     value.name = "username"
-  ##     value.uid = 1000
-  ##   do:
-  ##     # block is executed when `key` not in `t`
-  ##     raise newException(KeyError, "Key not found")
-  ##
+  runnableExamples:
+    type
+      User = object
+        name: string
+        uid: int
+
+    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
+    # 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
   var hc: Hash
   var index = rawGet(t, key, hc)