Using generic procedure with mixin can be dangerous?
Here is sample code: https://wandbox.org/permlink/u6C7eeR3sHlpNCcS
(I used Wandbox instead of Nim Playground as it supports multiple files)
generic.nim module exports a generic proc mygeneric that uses mixin variable and procedure defined in mod1.nim or mod2.nim. When mygeneric is instantiated with int type parameter and called in mod1.nim and mod2.nim, its mixin var and proc are bound to one in mod1.nim. So output of mygeneric called from mod2.nim comes from variable and proc in mod1.nim.
When I changed order of module in import statement like import mod2, mod1, output of mygeneric changed.
If generic proc that uses mixin is used in large project and instantiated with commonly used type, adding new call to it results in unexpected behavior as the mixin bound to a symbol in other module or calls to it in other module change behavior.
Is this bug? If a generic proc was called with same type in multiple modules, should they call same one proc? If so, if it uses mixin and each module have corresponding variable or proc but different definiton, how it should be bound?
Is this bug? If a generic proc was called with same type in multiple modules, should they call same one proc?
IMO yes and there is no bug here. When a generic instantiation is re-used, no scope checking is performed. How to change the language spec so that "generic sandwiches" and "mixin weirdnesses" can be avoided is not entirely clear yet. :-/
IMO yes and there is no bug here. When a generic instantiation is re-used, no scope checking is performed.
Then, fixing this problem seems hard. Maybe, restriction like "If a generic proc use mixin, One of type parameter must be a module local type" might fix it. But that might be inconvenient.