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:
dawidkotlin 2021-02-19 07:59:33 +01:00 • committed by GitHub
commit 95664e1524
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -54,6 +54,20 @@ runnableExamples:
## **Note:** If the type has a `==` operator, the following must hold:
## 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
## ========
## * `md5 module <md5.html>`_ for the MD5 checksum algorithm