3 ms·
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
by wk_end 2y 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 2y agoWith 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.nim https://github.com/nim-lang/Nim/lib/pure/collections/tables.... 1 - https://github.com/nim-lang/Nim/blob/652edb229ae3ca736b19474cd315f28a9e0893a2/lib/pure/collections/tables.nim#L236-L244 https://github.com/nim-lang/Nim/blob/652edb229ae3ca736b19474... 2 - https://nim-lang.org/docs/manual.html#generics-mixin-statement https://nim-lang.org/docs/manual.html#generics-mixin-stateme...