simpler, sweeter syntax for modules

brad dunbar dunbarb2 at gmail.com
Thu Mar 22 05:29:43 PDT 2012


This is great stuff.  I've got a quick question about the `as` keyword.

>> import 'foo.js' as Foo;

Is there any reason we can't simply do

>> var Foo = import 'foo.js';

When looking for the source of a variable, I often search for /Foo =/ to
discern it's origin and it only seems natural that giving a name to module
would use an assignment.  Furthermore, when combined with a destructuring
assignment it seems to simplify importing a modules export.  For instance

>> import foo from "foo.js";

can become

>> {foo} = import 'foo.js';

I could even see renaming being done in a similar fashion.

-brad dunbar


On Wed, Mar 21, 2012 at 6:28 PM, David Herman <dherman at mozilla.com> wrote:

> 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
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120322/3a5b8a0c/attachment-0001.html>


More information about the es-discuss mailing list