by the_mitsuhiko · 11y ago · 0 comments
Turns out other languages have evolved in the mean time and came up with much better solutions. In particular with dynamic languages ES6 modules in combination with npm's approach to package dependency management is the current benchmark. ES6 exposes bindings instead of values which solves a ton of issues that Python will never be able to fix with it's import system.
import {foo} from 'bar.js';
// you can't re-bind `foo` from here, but if
// a function defined in 'bar.js' mutates it,
// the change is reflected here too.
It enables circular dependencies, for free, and makes it easy to discard unused code.See the examples at the Rollup playground.
- consistent syntax - no circular dependencies - you can only import a module, only one symbol is polluting the namespace
I wonder what will be the winning solution for versioning.
npm and 260 char limit for path on Windows was driving me nuts.
1) inconsistent syntax: import "github.com/fsouza/go-dockerclient"; docker.NewClient...
Notice how you import something with the trailing path being "go-dockerclient" but the actual package name is "docker"?
In npm, if you type x = require("foo"), you always refer to it as 'x.something'. You don't have to guess or read docs.
2) build flags. What the fuck? // +build fuckme
3) Canonical import paths. Double what the fuck? For those of you who don't know, if you have "github.com/foo/bar" but write 'package bar // import "github.com/google/bar"' then a user of your package will not be able to compile it unless they move it to the directory you gave.
Yeah, no joke.... I'm in awe at this stupidity
4) Poor project-based importing (something actually worse than virtualenv; nothing near as good as npm).
5) init side effects, e.g. look at how pprof works. Literally you do an underscore import and it mutates the behaviour of the http package. Thanks.
2) Build flags are very useful, on the verge of required, for some situations. For instance, code that needs seperate implementations for each os.
3) This is very useful for packages that may be on github, but served through an alternate path. If they import the github version, you are now stuck with github. If you move to self hosted git or something, their existing codebase won't know how to do an update, and will require a rewrite to use the new version.
4) I'll be the first to admit it isn't the greatest, but, it's rendered mostly moot by the fact that you build single binaries. Deal with a slightly annoying import system in exchange for effortless deployments and not having to worry about libraries and such on the servers? Heck yeah!
5) I agree on this one, it should be something more like doing pprof.Register(*ServeMux) in an init in your program.
I prefer systems that (a) allow namespace nesting within a single package and (b) don't run arbitrary code on import (in Go's case, init()). The latter is particularly pernicious because systems that have arbitrary code execution on import have to define some kind of global ordering on imports (throughout the entire program!), since the order that packages get loaded in very much affects the program semantics. This is called the "static initialization order fiasco" in C++ and Go inherits it too.
If you can upgrade, node 5/npm 3 fixed the by flattening out the node_modules folder as much as possible.
Why not manage access to such things in a singleton class?
We had a decent sized library at a previous company that pulled in modules that defined huge register maps, wrapped c++ libraries, etc.
I wrapped all imports in a lazy importer that was triggered by the first attribute access. It brought our script startup times from 3 seconds down to a fraction.
Blows me away that this isn't default behavior for ALL modules.
You could I suppose do some cache warming to make sure the first user request isn't slowed down, but its one more thing to think about.
Well, putting code in the root of your file is generally the problem to such things, I would argue. Granted, I don't know about how that is necessary when it comes to "register maps" and "wrapped c++ libraries". But I'd imagine you should be encapsulating them away anyways and that would include fixing large startup time by design.
If you want hundreds on megs of shared mutable state, a database is a proper solution.
I'd like the program to finish in 15 seconds or less, please.
immutable singleton is fine. The other concern is performance, but if you don't have to do this, there is no point.