Module Comments

Herby Vojčík herby at mailbox.sk
Sun Dec 9 00:08:37 PST 2012



Domenic Denicola wrote:
>> -----Original Message-----
>> From: Andreas Rossberg [mailto:rossberg at google.com]
>> Sent: Thursday, December 6, 2012 11:31
>>
>> On 6 December 2012 16:44, Domenic Denicola
>> <domenic at domenicdenicola.com>  wrote:
>>> For the record, here's the idea Yehuda and I worked out:
>>>
>>> https://gist.github.com/1ab3f0daa7b37859ce43
>>>
>>> I would *really* appreciate if people read it (it's easy reading, I
>>> promise!) and incorporated some of our concerns and ideas into their
>>> thinking on module syntax.
>> However, the more radical parts of your proposal (allowing arbitrary export expressions, and arbitrary import patterns) do not work.
>>
>> The problem is that imports are not normal variable assignments. They do not copy values, like normal destructuring, they are aliasing bindings! If you were to allow arbitrary expressions and patterns, then this would imply aliasing of arbitrary object properties. Not only is this a completely new feature, it also is rather questionable -- the aliased location might disappear, because objects are mutable.
>
> Thanks for the feedback Andreas; this is really helpful. It took me a while to figure out what you meant by this, but I think I understand now. However, I think that since the bindings are const bindings, the difference between copying and aliasing is unobservable—is that right? Thus the mental model could be copying in all cases, both top-level and deeper.
>
> I think the symmetry I am looking for is that
>
> import { x: [a, b], f } from "foo";
>
> should work the same as
>
> import foo from "foo";
> const { x: [a, b], f } = foo;

No, afaicr that was not the idea, see below.

> And I realize the linked gist does not introduce const bindings, but instead let bindings; that's easy enough to fix if it's the right path forward.
>
>> You could also consider imports always meaning copying, but then you exporting a variable will no longer be useful.
>
> This is the part that made me question whether I understand what you're saying. What do you mean by "exporting a variable" and "useful"?

The part about "exporting a variable" is in fact explaining that it is 
not about const as you noted above (cp in unix PoV), but really copy of 
the binding (ln in unix PoV).

That is, when you in "foo"

   export let FOO = 42;

and do

   import { foo } from "foo";

in two places, then "foo = -1;" in one place means it is -1 in all other 
places as well.

I am right in what Andreas meant, then it leads me to the question: is 
it really meant to be that way? Isn't copying the value (used all over 
in the language) better way? If I want a shareable value, I export an 
object that has it as a property. Should export and import be by-reference?

Herby


More information about the es-discuss mailing list