simpler, sweeter syntax for modules

David Herman dherman at mozilla.com
Wed Mar 21 15:28:34 PDT 2012


H'lo,

Thanks to some a-maz-ing [1] work by Andreas Rossberg (I'll spare the gory algorithmic details), the linking process no longer needs the syntactic distinction that static module bindings are only created via the `module` (contextual) keyword. This frees us up to simplify the syntax, making much less use of the `module` keyword and much more use of `import`.

I've drafted a new syntax that is both much simpler and, I think, far more intuitive than the previous version.

Examples:

* importing an external module:

    import "foo.js" as Foo;

* importing a module's export:

    import foo from "foo.js";
    import bar from Bar;
    import baz from bar.mumble.quux;

* importing all exports:

    import * from Bar;

* importing with renaming:

    import { draw: drawGun }    from "cowboy.js",
           { draw: drawWidget } from "widgets.js";

* defining a module (same as ever):

    module m { ... }

* aliasing a module for convenience:

    module m = foo.bar.baz;

This new syntax is up on the wiki:

    http://wiki.ecmascript.org/doku.php?id=harmony:modules

A couple conventions to note about this:

* the "as" contextual keyword signifies renaming the module *itself*

* the "from" contextual keyword always signifies extracting an export /from/ the module

* while "import" no longer strictly means extracting an export, it matches common spoken usage better -- "import" is used both to mean extracting exports /and/ loading external modules

* despite this overloading, a single "import" declaration is *only* ever importing modules or importing bindings, never both

Comments welcome [2],
Dave

[1] Really. You have no idea.
[2] Translation: Unleash the hounds of bikeshedding! ;-P



More information about the es-discuss mailing list