add collect with infered init, refs #16078 fixes #14332 (#16089)

* changelog
* add testcase, fixes #14332
This commit is contained in:
Antonis Geralis 2020-12-03 21:32:18 +02:00 • committed by GitHub
commit 808ab7eae2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 88 additions and 37 deletions

View file

@ -40,7 +40,8 @@ import random
const b = @[0, 1, 2]
let c = b.dup shuffle()
doAssert c.len == 3
doAssert b[0] == 0
doAssert b[1] == 1
#test collect
import sets, tables
@ -83,12 +84,27 @@ let z = collect(newSeq):
else: d
assert z == @["word", "word"]
proc tforum =
let ans = collect(newSeq):
for y in 0..10:
if y mod 5 == 2:
for x in 0..y:
x
tforum()
block:
let x = collect:
for d in data.items:
when d is int: "word"
else: d
assert x == @["bird", "word"]
assert collect(for (i, d) in pairs(data): (i, d)) == @[(0, "bird"), (1, "word")]
assert collect(for d in data.items: (try: parseInt(d) except: 0)) == @[0, 0]
assert collect(for (i, d) in pairs(data): {i: d}) == {1: "word",
0: "bird"}.toTable
assert collect(for d in data.items: {d}) == data.toHashSet
# bug #14332
template foo =
discard collect(newSeq, for i in 1..3: i)
foo()