Hello guys,
I have been exploring the usage of modules, and I find that it's quite hard for Nim to deal with objects or procs that have the same name as the module where they are defined, when imported into other modules:
foo.nim
type foo* = object
type foo_foo* = object
bar.nim
import foo
export foo
baz.nim
import bar
let a = bar.foo_foo() #works, different name than the "foo" module
let b = bar.foo() #doesn't work, same name as the "foo" module
Is there a way to achieve this without having to rename the module or the object?
See also here: https://nim-lang.github.io/Nim/nep1.html
Thanks! I know all of that, I just submitted a basic test code from a much bigger project (https://github.com/vitreo12/omni) that I'm working on.
I just wondered of the general case of having idents with the same name of the module.
having idents with the same name of the module.
Is an excellent way to trigger mysterious bugs that seem to disappear when you want to report them in the issue tracker.
Unfortunately for now the best advice is "don't do that" but ideally I'd like a warning "[Warning]: Type 'foo' is shadowing a module 'foo'".
I find this feature annoying, and workaround like calling modules with plurals times.nim instead of time.nim. Or crawlers.nim instead of crawler.nim because otherwise it would prevent you from defining local variable let crawler = ....
Been thinking if it's worth to postfix files names with something like m as timem.nim to prevent this conflicts.
For your specific example changing bar.nim to the following works:
import foo as fu
export fu
I find this feature annoying. And workaround seems of calling modules with plurals times.nim instead of time.nim is not good.
When I started with Nim, I noticed that the module naming convention was the "opposite" of Python's, which prefers singular words as module names. However, once you just take it as a naming convention, it's no longer a problem (for me at least).
Sometimes, conventions are "just" conventions to be consistent, but some conventions also solve problems, like this plural naming convention for modules in Nim. This makes it even easier to stick to such conventions. :-)