forked from nim-lang/Nim
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implements nim-lang/RFCs#380
- Loading branch information
Showing
6 changed files
with
200 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import hashes | ||
|
||
type | ||
Obj* = object | ||
x*, y*: int | ||
z*: string # to be ignored for equality | ||
|
||
proc `==`*(a, b: Obj): bool = | ||
a.x == b.x and a.y == b.y | ||
|
||
proc hash*(a: Obj): Hash = | ||
!$(hash(a.x) !& hash(a.y)) | ||
|
||
type | ||
RefObj* = ref object | ||
x*, y*: int | ||
z*: string # to be ignored for equality | ||
|
||
proc `==`*(a, b: RefObj): bool = | ||
a.x == b.x and a.y == b.y | ||
|
||
proc hash*(a: RefObj): Hash = | ||
!$(hash(a.x) !& hash(a.y)) | ||
|
||
type | ||
GenericObj1*[T] = object | ||
x*, y*: T | ||
z*: string # to be ignored for equality | ||
|
||
proc `==`*[T](a, b: GenericObj1[T]): bool = | ||
a.x == b.x and a.y == b.y | ||
|
||
proc hash*[T](a: GenericObj1[T]): Hash = | ||
!$(hash(a.x) !& hash(a.y)) | ||
|
||
type | ||
GenericObj2*[T] = object | ||
x*, y*: T | ||
z*: string # to be ignored for equality | ||
|
||
proc `==`*(a, b: GenericObj2): bool = | ||
a.x == b.x and a.y == b.y | ||
|
||
proc hash*(a: GenericObj2): Hash = | ||
!$(hash(a.x) !& hash(a.y)) | ||
|
||
type | ||
GenericObj3*[T] = object | ||
x*, y*: T | ||
z*: string # to be ignored for equality | ||
GenericObj3Alias*[T] = GenericObj3[T] | ||
|
||
proc `==`*[T](a, b: GenericObj3Alias[T]): bool = | ||
a.x == b.x and a.y == b.y | ||
|
||
proc hash*[T](a: GenericObj3Alias[T]): Hash = | ||
!$(hash(a.x) !& hash(a.y)) | ||
|
||
type | ||
GenericObj4*[T] = object | ||
x*, y*: T | ||
z*: string # to be ignored for equality | ||
GenericObj4Alias*[T] = GenericObj4[T] | ||
|
||
proc `==`*(a, b: GenericObj4): bool = | ||
a.x == b.x and a.y == b.y | ||
|
||
proc hash*(a: GenericObj4): Hash = | ||
!$(hash(a.x) !& hash(a.y)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# https://github.com/nim-lang/RFCs/issues/380 | ||
|
||
from mobjhash import Obj, RefObj, GenericObj1, GenericObj2, GenericObj3, GenericObj4 | ||
import tables | ||
|
||
block: | ||
var t: Table[Obj, int] | ||
t[Obj(x: 3, y: 4, z: "debug")] = 34 | ||
doAssert t[Obj(x: 3, y: 4, z: "ignored")] == 34 | ||
doAssert Obj(x: 4, y: 3, z: "debug") notin t | ||
|
||
block: | ||
var t: Table[RefObj, int] | ||
t[RefObj(x: 3, y: 4, z: "debug")] = 34 | ||
doAssert t[RefObj(x: 3, y: 4, z: "ignored")] == 34 | ||
doAssert RefObj(x: 4, y: 3, z: "debug") notin t | ||
|
||
block: | ||
var t: Table[GenericObj1[float], int] | ||
t[GenericObj1[float](x: 3, y: 4, z: "debug")] = 34 | ||
doAssert t[GenericObj1[float](x: 3, y: 4, z: "ignored")] == 34 | ||
doAssert GenericObj1[float](x: 4, y: 3, z: "debug") notin t | ||
|
||
block: | ||
var t: Table[GenericObj1[int], int] | ||
t[GenericObj1[int](x: 3, y: 4, z: "debug")] = 34 | ||
doAssert t[GenericObj1[int](x: 3, y: 4, z: "ignored")] == 34 | ||
doAssert GenericObj1[int](x: 4, y: 3, z: "debug") notin t | ||
|
||
block: | ||
var t: Table[GenericObj2[float], int] | ||
t[GenericObj2[float](x: 3, y: 4, z: "debug")] = 34 | ||
doAssert t[GenericObj2[float](x: 3, y: 4, z: "ignored")] == 34 | ||
doAssert GenericObj2[float](x: 4, y: 3, z: "debug") notin t | ||
|
||
block: | ||
var t: Table[GenericObj3[float], int] | ||
t[GenericObj3[float](x: 3, y: 4, z: "debug")] = 34 | ||
doAssert t[GenericObj3[float](x: 3, y: 4, z: "ignored")] == 34 | ||
doAssert GenericObj3[float](x: 4, y: 3, z: "debug") notin t | ||
|
||
block: | ||
var t: Table[GenericObj4[float], int] | ||
t[GenericObj4[float](x: 3, y: 4, z: "debug")] = 34 | ||
doAssert t[GenericObj4[float](x: 3, y: 4, z: "ignored")] == 34 | ||
doAssert GenericObj4[float](x: 4, y: 3, z: "debug") notin t |