Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #8485 ; make default public instead of redefined everywhere; use better implementation #8490

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions lib/core/seqs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,6 @@ proc grow*[T](x: var seq[T]; newLen: int; value: T) =
x.data[i] = value
x.len = newLen

template default[T](t: typedesc[T]): T =
var v: T
v

proc setLen*[T](x: var seq[T]; newLen: int) {.deprecated.} =
if newlen < x.len: shrink(x, newLen)
else: grow(x, newLen, default(T))
Expand Down
4 changes: 0 additions & 4 deletions lib/pure/collections/queues.nim
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,6 @@ proc add*[T](q: var Queue[T], item: T) =
q.data[q.wr] = item
q.wr = (q.wr + 1) and q.mask

template default[T](t: typedesc[T]): T =
var v: T
v

proc pop*[T](q: var Queue[T]): T {.inline, discardable.} =
## Remove and returns the first (oldest) element of the queue `q`.
emptyCheck(q)
Expand Down
9 changes: 0 additions & 9 deletions lib/pure/collections/sets.nim
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ type

{.deprecated: [TSet: HashSet].}

template default[T](t: typedesc[T]): T =
## Used by clear methods to get a default value.
var v: T
v

proc clear*[A](s: var HashSet[A]) =
## Clears the HashSet back to an empty state, without shrinking
## any of the existing storage. O(n) where n is the size of the hash bucket.
Expand Down Expand Up @@ -281,10 +276,6 @@ template doWhile(a, b) =
b
if not a: break

template default[T](t: typedesc[T]): T =
var v: T
v

proc exclImpl[A](s: var HashSet[A], key: A) : bool {. inline .} =
assert s.isValid, "The set needs to be initialized."
var hc: Hash
Expand Down
4 changes: 0 additions & 4 deletions lib/pure/collections/tableimpl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,6 @@ template hasKeyOrPutImpl(enlarge) {.dirty.} =
maybeRehashPutImpl(enlarge)
else: result = true

template default[T](t: typedesc[T]): T =
var v: T
v

template delImplIdx(t, i) =
let msk = maxHash(t)
if i >= 0:
Expand Down
16 changes: 16 additions & 0 deletions lib/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4193,3 +4193,19 @@ when defined(genode):
componentConstructHook(env)
# Perform application initialization
# and return to thread entrypoint.

# needed pending https://github.com/nim-lang/Nim/issues/8486
proc defaultImpl[T]():T=
var a: T
result = a

template default*[T](t: typedesc[T]): T =
## returns default value for type ``T``, and can work at compile time; V2
runnableExamples:
const a = (1,2).type.default
doAssert a == (0, 0)
defaultImpl[T]()

when isMainModule:
# needed pending https://github.com/nim-lang/Nim/issues/7280
discard int.default
4 changes: 0 additions & 4 deletions tests/destructor/tcustomseqs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,6 @@ proc grow*[T](x: var myseq[T]; newLen: int; value: T) =
x.data[i] = value
x.len = newLen

template default[T](t: typedesc[T]): T =
var v: T
v

proc setLen*[T](x: var myseq[T]; newLen: int) {.deprecated.} =
if newlen < x.len: shrink(x, newLen)
else: grow(x, newLen, default(T))
Expand Down
17 changes: 17 additions & 0 deletions tests/system/tsystem_misc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,20 @@ proc foo(a: openArray[byte]) =

let str = "0123456789"
foo(toOpenArrayByte(str, 0, str.high))


# use `block default:` pending https://github.com/nim-lang/Nim/issues/8506
block default2:
doAssert int.default == 0

# test case from https://github.com/nim-lang/Nim/issues/8485
proc len(T: typedesc[tuple]):auto=
const t=default(T)
var n=0
for _ in t.fields:
inc n
return n

var a=(1,2,3)
const length = a.type.len
doAssert length == 3
4 changes: 2 additions & 2 deletions tests/vm/tvmmisc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ block:

# #4412
block:
proc default[T](t: typedesc[T]): T {.inline.} = discard
proc default2[T](t: typedesc[T]): T {.inline.} = discard

static:
var x = default(type(0))
var x = default2(type(0))

# #6379
import algorithm
Expand Down