▲ | wk_end 3 days ago | |
If I’m writing a hash map module that’s generic over what it accepts, and later someone else is going to want to use it with Foo keys, how am I able to write my hash map such that the hash function for Foos are in scope in my hash map module? | ||
▲ | archargelod 3 days ago | parent [-] | |
With function overloading, templates and late static binding. You just use a `hash` function in your library code and user has to implement a version of it that accepts the Foo type. To resolve the scope problem, Nim uses templates[1] and a `mixin`[2] statement to bind user-defined hash procedure. 0 - https://github.com/nim-lang/Nim/lib/pure/collections/tables.... 1 - https://github.com/nim-lang/Nim/blob/652edb229ae3ca736b19474... 2 - https://nim-lang.org/docs/manual.html#generics-mixin-stateme... |