simpler, sweeter syntax for modules
dherman at mozilla.com
Wed Mar 21 17:25:35 PDT 2012
On Mar 21, 2012, at 3:44 PM, Luke Hoban wrote:
> Great to see the updates. A couple of questions:
>>> import "foo.js" as Foo;
>>> import foo from "foo.js";
> These two forms look rather confusingly similar given how different they are, and the inversion of order of where the filename lives doesn't seem to line up with the semantic difference between the two forms. It feels like this is an attempt towards fluent syntax design, but to me at least, this just doesn't feel easy to learn/remember/read.
Yeah, I'm not trying to do fluent syntax per se; the goal is not COBOL/AppleScript. But it should still read well, in such a way as to avoid the off-by-one confusion inherent in being able to import a module or import its exports.
Andreas also didn't like the inversion of order. I came to this because earlier versions of the syntax were inconsistent about whether `from` meant the-module-itself or an-export-of-the-module, which made it even more confusing. This new syntax was inspired by Python, but with `import` as the leading keyword, rather than `from`. Which is what leads to the inversion of order.
I've gone through a few alternatives, but they all read more awkwardly to me:
import Foo at "foo.js"; // ...maybe? *shrug*
import Foo is "foo.js"; // reads awkwardly
module Foo = "foo.js"; // looks like Foo is a string
module Foo is "foo.js"; // just looks like an awkward workaround
>>> module m = foo.bar.baz;
> Why isn't aliasing a remote module the same as aliasing a local module? That is, why can't I say 'module m = "foo.js"' instead of 'import "foo.js" as m'? It seems the whole syntactic surface area of modules would be simpler if remote module identifiers were used in the same way as local module bindings in the various syntactic forms. Since this seems to be the way things are done on the RHS of 'import...from...', why be different in 'module...=...'? I thought this is how things were originally - why change?
Perhaps. I saw community members upset that we would use `module` as the keyword for importing external modules instead of `import`, e.g.:
Moreover, Brendan felt that module...=..."..." looks too confusingly like you're assigning a string value. Others I spoke to, particularly those accustomed to Python, felt that `import` read better as the way to load external modules.
> More broadly - is it possible to reduce further to just 'module...=...' and 'import...from...'? Which mean "alias a module" and "import names from inside a module" respectively.
Certainly possible, yes. I think that's Andreas's preferred syntax as well. I think that's a viable alternative. Just based on my anecdotal experience talking to people so far, I suspect we'd find people's preferences split. The good news is that we are no longer constrained by technical details of the linking semantics as to which way we want to go. The bad news is that we'll have to pick a syntax, and some people will undoubtedly hate it. :-P
Seriously, I don't feel religious about either of the two syntaxen. I think I will put up both alternatives on the wiki for a better head-to-head comparison.
Thanks for the feedback,
More information about the es-discuss