While I like how elegant Nim is, there is one thing which seems, not elegant enough. That is the importing system. I would like to propose a import a with "b" expression which will anable importing a.xyz as bxyz
Currently, import a will import everything with an * (maybe xyz as an example) and allowing access without a prefix of a.. While this works handy for many aspects, such as Uniform function call syntax, it is troublesome in some occasions.
As a solution, I would like to propose the import with expression. Of course, the with can be anything. What this enables is to add a prefix (or maybe suffix) to the imported identifiers. For example, every identifier in module a can be used like bxyz if you import a with b.
While I believe this is a smart solution, I notice some defects.
Any comments?
or maybe the with can be cleaver enough to follow the original form
Yes, it is the easiest part - just to change case of the first letter of the prefix according to identifier, and maybe uppercase identifier's first letter.
How to handle operators.
Just to import them as is? Or an optional second (non-alphanumeric) prefix for operators?
Thanks for the reply. I have nothing special to say about the case problem.
Just to import them as is? I can't agree on this since whoever tried to use the import with expression is interested in dividing namespaces.
optional second (non-alphanumeric) prefix for operators? This seems to be a better idea, and most realistic solution. My concern are
I still like my old proposal for the use keyword (instead of from ... import nil). Then import means "import into namespace"; use means "use with a prefix".
Both can be used with the from, except, and as keywords:
from mymodule import myproc # put myproc() in global namespace
from mymodule use myproc # must use mymodule.myproc()
use mymodule except mybadproc, myotherproc
use mymodule as m
from mymodule import myproc as mygoodproc
That last line proposes the ability to specify the name of identifier being imported.
Like I just said in the dir thread, maybe Nim needs a stdlib module for compile-time module introspection (similar to what nimsuggest does externally).
You could then combine those two features, (dir and from mymodule [use|import] X as Y) to write whatever macros you want, like:
import macros
macro importWithPrefix*(moduleName, prefix: string): untyped =
result = newStmtList()
for id in dir($moduleName):
let targetId = $prefix & id
result.add parseStmt("from " & $moduleName & " import " & id & " as " & targetId)
(NOTE: just conceptual pseudocode here...)