import * as operations from "Math";
Wow, I knew import syntax with a separate from
statement could be awkward, but that’s a new one for me.
Apparently, the *
cannot be used to import all symbols underneath a module (you always have to specify the as moduleName
), so I guess, that makes it somewhat less weird for referring to the module itself.
From what I can tell, there’s also no obvious other keyword they could’ve used:
package
is only a keyword in strict mode.self
is not a keyword.this
is kind of awkward.- Leaving out the keyword is kind of awkward (
import as operations from "Math";
). - Changing up the whole syntax for this one case is awkward, too (
import from "Math" as operations;
).
So, I guess, I’ll allow it, but I’m still not happy about it…
Oh yeah, I was merely complaining about the syntax. Coming from other languages, I interpreted that import statement to mean essentially this:
import { double, exponent /*...*/ } as operations from "Math";
…and as such, it took me a few seconds to understand what’s being aliased by
as operations
.As for importing all symbols of a module, I do think it’s more harm than good in non-compiled languages. But when it comes to compiled languages, I’d say it depends on the language.
In Rust, for example, I can easily throw down an inline module with one-way isolation, so that it can transparently access everything in its parent module via
use super::*;
, while the parent module can’t access what’s in the module (unless it’s been markedpub
). That can reduce mental complexity of the code (and is actually used a lot, because unit tests are typically put into such an inline module).It’s also useful in Rust, because you can re-export symbols in different modules, so you can break up a file without breaking the imports by throwing a
pub use my_sub_module::*;
into the original module.But yeah, on the flipside, I really wouldn’t miss it, if it didn’t exist in Java. It was rather even annoying, because the popular IDEs have a rule to replace explicit imports with an asterisk as soon as it reached 5 symbols imported from the same module.
It’s not as bad as one might think, because you can’t declare top-level functions or variables in Java (everything has to be in a class), but it still sometimes led to those asterisk imports bringing in the wrong class names, so I’d have to manually add the import I wanted underneath them…