jsony
jsony copied to clipboard
Bizzare import interaction resulting in `attempting to call undeclared routine: 'toJson'`.
This is probably a compiler error, but I wasn't able to come up with a reproduction without jsony.
Nim version: 2.2.0 Backend: c
imported.nim
import pkg/jsony
type
Foo* = ref object
foo: float # Remove this line and everything works 🤡
proc serialize*(msg: Foo|float) =
discard toJson(msg)
main.nim
import imported
# import jsony # Add this line and everything works 🤡
serialize(Foo())
Output
...template/generic instantiation of `toJson` from here
/Users/dawid/.nimble/pkgs2/jsony-1.1.5-6aeb83e7481ca8686396a568096054bc668294df/jsony.nim(881, 11) template/generic instantiation of `dumpHook` from here
/Users/dawid/.nimble/pkgs2/jsony-1.1.5-6aeb83e7481ca8686396a568096054bc668294df/jsony.nim(823, 6) template/generic instantiation of `dumpHook` from here
/Users/dawid/.nimble/pkgs2/jsony-1.1.5-6aeb83e7481ca8686396a568096054bc668294df/jsony.nim(801, 8) template/generic instantiation of `dumpKey` from here
/Users/dawid/.nimble/pkgs2/jsony-1.1.5-6aeb83e7481ca8686396a568096054bc668294df/jsony.nim(739, 15) Error: attempting to call undeclared routine: 'toJson'
I think I know what is going on; this is a bit of a mind-bendy issue. When you define a non-generic proc, it can use the procs around it, like dumpHook, dumpKey, etc. However, when you make a generic proc with (a | b), it can't know which functions it will call. So, when you do call it, it can't find dumpHook or dumpKey in the local namespace. Therefore, you need to import it again. It's just how generics and imports work.