From 6ec9c7f683abd520ad70b7ca6fbee11312b5cf11 Mon Sep 17 00:00:00 2001 From: Tomohiro Date: Mon, 3 Apr 2023 12:15:14 +0900 Subject: [PATCH] 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 --- lib/system.nim | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/system.nim b/lib/system.nim index af7e0e4c8d..3a24870047 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)