4 ms·
I played with Nim this weekend. It's a shame that it did so many things right yet went with unqualified imports by default. After being spoiled by other langu
by hombre_fatal 6y ago
I played with Nim this weekend.
It's a shame that it did so many things right yet went with unqualified imports by default.
After being spoiled by other languages, something even Node.js got very right, I just don't want to try to get work done anymore in a language where you go `import Lib` and then a bunch of unqualified identifiers like `foo()` are available in the code.
I liked that Ruby feature as a beginner. But it didn't take long for me to realize that knowing where things come from is worth the 500ms one-time-cost you spend qualifying each import.
- oehtXRwMkIs 6y agoThat always bothered me too but never saw any good justification for that. I feel like it's just a straight up flaw in the language, unless someone can tell me why you would ever want unqualified imports.
- elcritch 6y agoIt hasn't bothered me with types and auto-complete combined with unified-call syntax (UCS) after a bit of use. Not sure if UCS and qualified imports would work so well. You can import specific names (similar to Python) like `import posix as ps`. Is that what you mean by qualified imports?
- Varriount 6y agoSee https://news.ycombinator.com/item?id=23452857 https://news.ycombinator.com/item?id=23452857 In short: unqualified imports don't play well with operator overloading, templates, or generics.
- curioussavage 6y agoNothing stopping you from using them in your own code. I prefer qualified too.
- sergiotapia 6y agoI code Nim for fun some weekends and agree that totally sucks, and what's worse it seems like it's idiomatic to write Nim that way. However, for us we can do: from "./crawlers/leetx.nim" import nil leetx.fetchLatest() That forces you to use the qualified version and doesn't contaminate your space.
- Varriount 6y agoNim supports both qualified and unqualified imports[0]. One uses `from module_name import nil`. Then every access to members of that module must be qualified. From my viewpoint (I'm sure there are others) I see two main reasons why unqualified imports are the default: operators and templates/generics. First, operators[1]. In Nim, one can implement an operator for any type, without extending that type, and without even implementing the operator in the same module as the type definition. If qualified imports were the default, then a + b would by default be module_name.`+`(a, b) (The backticks are used to force the plus sign to be recognized as an identifier) Yes, one could hypothetically perform an "unqualified import" for that specific symbol, but what if you have 5+ operators to import (like for a math library)? It would get tedious rather quickly. Now, templates. If you're not familiar with Nim, templates are similar to C templates, except that rather than a template performing textual substitution, it performs syntax tree substitution. As an example, this is how "less than" is implemented: template `>`*(x, y: untyped): untyped = ## "is greater" operator. This is the same as ``y < x``. y < x If qualified imports were the default, templates would be almost useless. Something like this: template toJsonString(value: untyped): untyped = $(toJson(value)) where `$` turns its argument into a string, and toJson turns its argument into a JSON node type, would fail because the template doesn't qualify any of those procedures. The same reasoning for templates also applies to generics (and might be a bit more familiar). A generic doesn't know where its input type comes from ahead of time, so it can't know how to qualify function calls. Python, C#, and other class based languages deal with these complications by using classes as a kind of "automatic unqualified import" signal. Though it doesn't have classes, Go uses a similar mechanism (explicit methods types that must be defined in the same package as the type implementation). Since Nim has neither method types nor classes, it just uses unqualified imports. I agree that unqualified imports are a pain for code navigation, but with the right tooling that navigation can be made easier (I know there's a Nim plugin for VSCode, and I believe there's ones for Vim and Emacs?) [0] https://nim-lang.org/docs/manual.html#modules-from-import-statement https://nim-lang.org/docs/manual.html#modules-from-import-st... [1] https://nim-lang.org/docs/manual.html#syntax https://nim-lang.org/docs/manual.html#syntax [2] https://nim-lang.org/docs/manual.html#templates https://nim-lang.org/docs/manual.html#templates
- hombre_fatal 6y ago
- larme 6y agothere's an article[0] addressing this topic though I'm not fully convinced [0] https://narimiran.github.io/2019/07/01/nim-import.html https://narimiran.github.io/2019/07/01/nim-import.html
- parhamn 6y agoIn short: - You can do python style by doing `from x import nil`, then all imports are package qualified. (Con: style enformcenet) - Ambiguous overrides are compiler errors in Nim (import has an 'except' keyword to omit some), so import overrides isn't as much of an issue. (Con: upstream package can break your compile by introducing the same function name) - Dynamic dispatch works across modules so its nice to just do sqrt() instead of math.sqrt() and complex.sqrt() (Con: this feels meh to begin with). Shrug.
- arc619 6y agoThe tl;dr is that Nim is statically typed so two functions that take different types are not ambiguous. If there is something that takes the same parameters, of the same type, in the same order it wont compile and you would use the module prefix to disambiguate. In over 5 years of coding in Nim this came up once when two libraries defined a `Point` type. They were subsequently given distinct names so i didnt even need it for long. Other options for ambiguous calls are renaming things during import with `import thing as othername` or using `type SdlPoint = sdl2.point` and so on, but i have never needed to do this. The main point is that its a compile time error to try to call an ambiguous thing. Btw mouse over in VSCode shows you where a symbol is defined too.
- planetis 6y agoExactly, this is when this problem pops up. Imo the best way to avoid this is to do: ``import sdl2 except Point`` and use the version of the procs that don't use Point, so you pass your own type as: ``render.drawPoints(p.x, p.y)``
- planetis 6y agoI've been programming exclusively in Nim for 4 years, never had a problem with it. In fact it's a lot easier not to think were to get each symbol both in terms of refactoring and mental overhead. Good editor support means, with a mouse over any symbol, you can see were it was declared. But I don't use nimsuggest, or nimlsp, I simply never have to think about it, in my own codebase. I can understand it can be helpful when working in big projects like the nim compiler itself but I'm talking about goto definition not "qualified" imports, those are useless...