From 95664e15247d678476d64a1c81f7c19b163e823c Mon Sep 17 00:00:00 2001 From: dawidkotlin <75385975+dawidkotlin@users.noreply.github.com> Date: Fri, 19 Feb 2021 07:59:33 +0100 Subject: [PATCH] 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 --- lib/pure/hashes.nim | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/pure/hashes.nim b/lib/pure/hashes.nim index 3cf0a8f82c..54e083d251 100644 --- a/lib/pure/hashes.nim +++ b/lib/pure/hashes.nim @@ -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 `_ for the MD5 checksum algorithm