summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorTomohiro <gpuppur@gmail.com>2023-04-03 12:15:14 +0900
committerGitHub <noreply@github.com>2023-04-03 05:15:14 +0200
commit6ec9c7f683abd520ad70b7ca6fbee11312b5cf11 (patch)
tree87f714b5f70cd3b43591de4c4b553aa2a17be89e /lib
parent63b4b3c5b8c930ffc271c5e4e1a446e8616b2571 (diff)
downloadNim-6ec9c7f683abd520ad70b7ca6fbee11312b5cf11.tar.gz
Fix example code of proc add*[T](x: var seq[T], y: sink openArray[T]) (#21607)
* Fix example code in system.nim

* Add example code to lib/system.nim

* Fix compile error

* Fix example code that can be unsafe
Diffstat (limited to 'lib')
-rw-r--r--lib/system.nim17
1 files changed, 13 insertions, 4 deletions
diff --git a/lib/system.nim b/lib/system.nim
index af7e0e4c8..3a2487004 100644
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -1108,6 +1108,11 @@ when defined(nimscript) or not defined(nimSeqsV2):
     ## containers should also call their adding proc `add` for consistency.
     ## Generic code becomes much easier to write if the Nim naming scheme is
     ## respected.
+    ##   ```
+    ##   var s: seq[string] = @["test2","test2"]
+    ##   s.add("test")
+    ##   assert s == @["test2", "test2", "test"]
+    ##   ```
 
 when false: # defined(gcDestructors):
   proc add*[T](x: var seq[T], y: sink openArray[T]) {.noSideEffect.} =
@@ -1142,13 +1147,17 @@ else:
     ## containers should also call their adding proc `add` for consistency.
     ## Generic code becomes much easier to write if the Nim naming scheme is
     ## respected.
-    ##   ```
-    ##   var s: seq[string] = @["test2","test2"]
-    ##   s.add("test") # s <- @[test2, test2, test]
-    ##   ```
     ##
     ## See also:
     ## * `& proc <#&,seq[T],seq[T]>`_
+    runnableExamples:
+      var a = @["a1", "a2"]
+      a.add(["b1", "b2"])
+      assert a == @["a1", "a2", "b1", "b2"]
+      var c = @["c0", "c1", "c2", "c3"]
+      a.add(c.toOpenArray(1, 2))
+      assert a == @["a1", "a2", "b1", "b2", "c1", "c2"]
+
     {.noSideEffect.}:
       let xl = x.len
       setLen(x, xl + y.len)