add example of hashing an object by all of its fields with fields (#16643)
* add example of hashing an object by all of its fields with `fields` * Update lib/pure/hashes.nim * Update lib/pure/hashes.nim * Update lib/pure/hashes.nim Co-authored-by: flywind <43030857+xflywind@users.noreply.github.com> Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>
This commit is contained in:
parent
9450bf6c08
commit
95664e1524
1 changed files with 14 additions and 0 deletions
|
|
@ -54,6 +54,20 @@ runnableExamples:
|
||||||
## **Note:** If the type has a `==` operator, the following must hold:
|
## **Note:** If the type has a `==` operator, the following must hold:
|
||||||
## If two values compare equal, their hashes must also be equal.
|
## If two values compare equal, their hashes must also be equal.
|
||||||
##
|
##
|
||||||
|
## You can hash an object by all of its fields with the `fields` iterator:
|
||||||
|
runnableExamples:
|
||||||
|
proc hash(x: object): Hash =
|
||||||
|
for f in fields(x):
|
||||||
|
result = result !& hash(f)
|
||||||
|
result = !$result
|
||||||
|
|
||||||
|
type
|
||||||
|
Obj = object
|
||||||
|
x: int
|
||||||
|
y: string
|
||||||
|
|
||||||
|
doAssert hash(Obj(x: 520, y: "Nim")) != hash(Obj(x: 520, y: "Nim2"))
|
||||||
|
|
||||||
## See also
|
## See also
|
||||||
## ========
|
## ========
|
||||||
## * `md5 module <md5.html>`_ for the MD5 checksum algorithm
|
## * `md5 module <md5.html>`_ for the MD5 checksum algorithm
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue