summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/system.nim9
-rw-r--r--lib/system/seqs_v2.nim2
2 files changed, 11 insertions, 0 deletions
diff --git a/lib/system.nim b/lib/system.nim
index 1b6b3c9a8..ed1a0077e 100644
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -24,6 +24,9 @@
 
 include "system/basic_types"
 
+func zeroDefault*[T](_: typedesc[T]): T {.magic: "ZeroDefault".} =
+  ## returns the default value of the type `T`.
+
 include "system/compilation"
 
 {.push warning[GcMem]: off, warning[Uninit]: off.}
@@ -911,6 +914,7 @@ proc default*[T](_: typedesc[T]): T {.magic: "Default", noSideEffect.} =
   # note: the doc comment also explains why `default` can't be implemented
   # via: `template default*[T](t: typedesc[T]): T = (var v: T; v)`
 
+
 proc reset*[T](obj: var T) {.noSideEffect.} =
   ## Resets an object `obj` to its default value.
   obj = default(typeof(obj))
@@ -2753,3 +2757,8 @@ when notJSnotNims and not defined(nimSeqsV2):
       moveMem(addr y[0], addr x[0], x.len)
       assert y == "abcgh"
     discard
+
+proc arrayWith*[T](y: T, size: static int): array[size, T] {.noinit.} = # ? exempt from default value for result
+  ## Creates a new array filled with `y`.
+  for i in 0..size-1:
+    result[i] = y
diff --git a/lib/system/seqs_v2.nim b/lib/system/seqs_v2.nim
index 42d9938c5..40fd50b48 100644
--- a/lib/system/seqs_v2.nim
+++ b/lib/system/seqs_v2.nim
@@ -125,6 +125,8 @@ proc setLen[T](s: var seq[T], newlen: Natural) =
       if xu.p == nil or xu.p.cap < newlen:
         xu.p = cast[typeof(xu.p)](prepareSeqAdd(oldLen, xu.p, newlen - oldLen, sizeof(T), alignof(T)))
       xu.len = newlen
+      for i in oldLen..<newlen:
+        xu.p.data[i] = default(T)
 
 proc newSeq[T](s: var seq[T], len: Natural) =
   shrink(s, 0)