Skip to content

Commit

Permalink
feat: hash()
Browse files Browse the repository at this point in the history
  • Loading branch information
litlighilit committed Jun 8, 2024
1 parent 2baf24e commit be77994
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ Uninstall with `nimble uninstall pylib`.
- [x] `format()`
- [x] `getattr()`
- [x] `hasattr()`
- [x] `hash()`
- [x] `hex()`
- [x] `id()`
- [x] `input()`
Expand Down
2 changes: 1 addition & 1 deletion src/pylib/builtins.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ import ./private/trans_imp

impExp builtins,
input, print, mathfunc, complex, misc, oopFuncs, attr,
iters, pyrange, pyslice, list, dict, set
iters, pyrange, pyslice, list, dict, set, pyhashes
28 changes: 28 additions & 0 deletions src/pylib/builtins/pyhashes.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

import std/hashes
import ./[
list, dict, set,
complex
]
import ../pystring/strimpl
import ../pybytes/bytesimpl

template toInt(h: Hash): int = int(h)
template hashable(typ){.dirty.} =
proc hash*(x: typ): int = toInt hashes.hash(x)

hashable int
hashable float
hashable PyComplex
hashable PyStr
hashable PyBytes
hashable tuple
hashable proc

const unhashErr = "TypeError: unhashable type: "
template unhashable(typ){.dirty.} =
proc hash*(x: typ): int{.error: unhashErr & $typ.}

unhashable PyList
unhashable PyDict
unhashable PySet

0 comments on commit be77994

Please sign in to comment.