simple modules

David Herman dherman at mozilla.com
Mon Feb 1 08:55:05 PST 2010


Hi Vassily, thanks for the feedback.

> It should be
> 
> <script type="harmony">
> 
> // import everything as Math
> import "Math";
> 
> alert("2π = " + Math.sum(Math.pi, Math.pi));
> 
> </script>

This is already possible with the `import "Math" as Math' form (which incidentally can easily be compiled to be exactly as efficient). Leaving the "as Math" part implicit doesn't work if the module specifier is not syntactically an identifier:

    import "@#$!";
    @#$!.mumble("grunch")

> We already have "with" for polluting local namespace, 
> and short syntax for such polluting doesn't feel right.

That's an inappropriate comparison, for two critical reasons. First: `with' *dynamically* changes the environment, so it destroys lexical scope, whereas when you import everything from a second-class module, it is still possible to know statically what bindings are in scope. Second: there's no contention, since it's a static error to import two modules that bind the same name. So conflicts are ruled out, and there's no ambiguity in a valid program.

Now, I recognize that some people feel that stylistically the "import everything" approach is bad /style/, and that programmers ought to list all their imports explicitly. But at the same time, this is still a scripting language, and it has to be convenient. There's a difference between advocating a style and forcing it. And if it imposes too heavy a burden you get nasty unintended consequences (e.g., nobody uses modules at all).

> Some longer syntax would be better, e.g.
> 
> <script type="harmony">
> 
> // import everything
> import "Math" as this;
> 
> alert("2π = " + sum(pi, pi));
> 
> </script>

That would only work with at most one module.

Dave



More information about the es-discuss mailing list