fix #17385, len must be declared before items (#17386)

This commit is contained in:
Miran 2021-03-15 18:45:10 +01:00 • committed by GitHub
commit 40a5d6537a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 21 deletions

View file

@ -164,6 +164,27 @@ proc contains*[A](s: HashSet[A], key: A): bool =
var index = rawGet(s, key, hc) var index = rawGet(s, key, hc)
result = index >= 0 result = index >= 0
proc len*[A](s: HashSet[A]): int =
## Returns the number of elements in `s`.
##
## Due to an implementation detail you can call this proc on variables which
## have not been initialized yet. The proc will return zero as the length
## then.
runnableExamples:
var a: HashSet[string]
assert len(a) == 0
let s = toHashSet([3, 5, 7])
assert len(s) == 3
result = s.counter
proc card*[A](s: HashSet[A]): int =
## Alias for `len() <#len,HashSet[A]>`_.
##
## Card stands for the `cardinality
## <http://en.wikipedia.org/wiki/Cardinality>`_ of a set.
result = s.counter
proc incl*[A](s: var HashSet[A], key: A) = proc incl*[A](s: var HashSet[A], key: A) =
## Includes an element `key` in `s`. ## Includes an element `key` in `s`.
## ##
@ -357,27 +378,6 @@ proc clear*[A](s: var HashSet[A]) =
s.data[i].hcode = 0 s.data[i].hcode = 0
s.data[i].key = default(typeof(s.data[i].key)) s.data[i].key = default(typeof(s.data[i].key))
proc len*[A](s: HashSet[A]): int =
## Returns the number of elements in `s`.
##
## Due to an implementation detail you can call this proc on variables which
## have not been initialized yet. The proc will return zero as the length
## then.
runnableExamples:
var a: HashSet[string]
assert len(a) == 0
let s = toHashSet([3, 5, 7])
assert len(s) == 3
result = s.counter
proc card*[A](s: HashSet[A]): int =
## Alias for `len() <#len,HashSet[A]>`_.
##
## Card stands for the `cardinality
## <http://en.wikipedia.org/wiki/Cardinality>`_ of a set.
result = s.counter
proc union*[A](s1, s2: HashSet[A]): HashSet[A] = proc union*[A](s1, s2: HashSet[A]): HashSet[A] =
## Returns the union of the sets `s1` and `s2`. ## Returns the union of the sets `s1` and `s2`.

11
tests/sets/m17385.nim Normal file
View file

@ -0,0 +1,11 @@
import std/sets
type
Diff*[T] = object
data: T
proc test*[T](diff: Diff[T]) =
var bPopular = initHashSet[T]()
for element in bPopular.items():
echo element

4
tests/sets/t17385.nim Normal file
View file

@ -0,0 +1,4 @@
import m17385
let a = Diff[int]()
a.test()