You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
reflection: imperative equivalence for declarative import #36
as today, we only have loader.import('./foo.js').then(m => {...}), which is reflective to import * as m from "./foo.js", as we discussed earlier today, we might need to provide reflection for the other two options (default and named exports import declarations).
To clarify, or to raise the question, depending on how you look at this, but local.import("./foo.js").then(def => ...) is really not reflective as it is the current proposal, essentially because def will not get updated if the value change in the provider module.
Yes, I'm with @arv on this, es6 with make this elegant enough without the need for multiple methods/signatures. loader.import should return the module. import/export is already complicated enough (I still have to reference this all the time), let's not muddle the dynamic api as well.
@arv@matthewp A low-level reflection API can be minimal and general. But the local import object is a high level API, meant for common use. And it's meant to be the dynamic analog of what you can do with the declarative syntax. It should have its defaults be symmetric with the surface syntax.
In the surface syntax, when you import a default, you don't have to write d-e-f-a-u-l-t. It's just the simplest thing where you have to say the least. In short, we want to allow:
Another way of looking at this is that single-export needs to be as convenient in this API as it is today in existing JS module systems. Not having to say default explicitly is very important to the ergonomics.
But we also have to allow importing the namespace object, of course. And sometimes a module will not have a default export, so forcing you to write .then((dummy, named) => ...) is obnoxious for those cases. Hence a second method specifically for asking for the namespace object.
@caridy@domenic It's important to understand the use case of local import as not being about reflection but rather about dynamic loading. Reflection is a low-level operation meant for when you're trying to extend the base semantics of the language/system. Dynamic loading is simply a thing apps commonly need to do.
Also keep in mind that .import will not resolve the promise until the module has been initialized, fully executing its top-level code. The overwhelming majority of the time, the aliasing behavior is only important for the initialization phase where top-level bindings are being set up. It's extremely rare to mutate top-level exports after that point. So it's inappropriate to optimize this API around that as a constraint.
Again, the low-level loader API should fully generalize the semantics, and there it is certainly sensible to have operations that just produce the namespace object as the most general thing. But the purpose of the local import API is as a high-level API whose ergonomics and style should mirror the ergonomics and style of the declarative syntax.
Not having to say default explicitly is very important to the ergonomics.
Yes
It's extremely rare to mutate top-level exports after that point.
Agree. We can probably make the case that the value of the default export should never change, in which case something like local.import("./foo.js").then(def => ...) should be fine.
You have to say default all the time already, it's a common way to export things. I see your point about having symmetry between the forms. But the declarative form is nice because of syntactical affordances you won't have here.
Question,
local.import("./foo.js").then((def,named)=> ...)
even valid? I thought Promise only had a single return value.
You have to say default all the time already, it's a common way to export things.
The important distinction is that you only say default inside the implementation of a module, just as you have to do something explicit in CommonJS/Node (module.exports = ...;). But on the consumer side, you don't have to think about it having a name. Anyway sorry to beat a dead horse; it sounds like you agree the symmetry is important.
I thought Promise only had a single return value.
LOLOLOL. (At least I'm slightly comforted by the fact that the lead of the promises spec didn't catch this either. ;-P)
I think a reasonable alternative is two forms, with the short name providing the default, and the longer name providing the namespace object:
still we should clarify whether and how people will be able to update the default export from within a module, if there is no way to do that, then this syntax will be perfect.
That's interesting, I hadn't heard of this feature. I'll have to dive into esdiscuss then, but I think I understand what you're saying.
I like the idea overall and agree it's a clever way of working around this problem. You do still want access to the loader within a module, import.local maybe.
This look pretty elegant!
Everything suddenly becomes very clean if we can go this way.
Don't be too frustrated: We can restart the naming discussion around this syntax! :) :)
Promise.all(['Spinner','Slider','Gallery'].map(import)).then(...)// Default import, call import as a functionimport.namespace('fs').then(fs=> ...)// Namespace importimport('fs','readFile').then(readFile=> ...)// New idea: Named import shortcut
You tell me if treating import as a function does even work. I just assume that it's possible since the static syntax always includes the from part, which should enable parsers to tell the two apart. Parsers are probably able to tell the two apart because the static import is never valid ES5 and this is except for the reserved keyword thing. Not sure if we want to go this way, but I'm proposing it anyway :)
Does this need to be a metaproperty and not some form of a function? It seems like a language extension would mean this loader would have to be the backer for all host environments if it assumes it gains meta-properties.
@matthewp it has different implications semantically than existing code/systems (couchdb, webworkers, mongodb, node, ringojs, ...) which is the issue. having the semantics able to match would be nice as well as be much safer for upgrade purposes
I agree, a breaking change for those legacy systems is probably a no-go. I wish there were more participation here from all interested parties but this repo has been largely dark for the last several months.
There's a specification at amodro-lifecycle that attempts to address some of these legacy needs such as Node, I would suggest giving it a read if you have time.
@matthewp my last experience here did not include metaproperties or requirements for it to be used as the import hook handler for VMs. WebAssembly/design#256 brought me here made me see several changes like this issue which make it a much larger scope than I thought it was. my interest came as a result of the increased scope and WASM making me come reread it in the current form
The metaproperty proposal only exists in this issue and hasn't been written to any spec AFAIK. There hasn't been any increase in scope to my knowledge, it is now much smaller is scope actually.
@matthewp we commented on it because it is a language extension which immediately concerned us. A lot of work is often done in conversations and only shows up in specs to replace TODOs etc. at a very late stage if we want to read public available text. I think talking in issues is fairly similar to talking about the spec.
@matthewp@bmeck just a side note, the import imperative call is bound to the loader instance that loaded the host module in the first place (just like require() is bound to the host module), which means all modules will have access to it since they all have a loader associated, even if it is the built-in default loader implemented by the runtime.
Activity
domenic commentedon Mar 12, 2015
I really like the
(def, named) =>signature.What is different between
local.importNamespace("./foo.js").then(m => use(m))vs.local.import((def, named) => use(named))?johnjbarton commentedon Mar 12, 2015
(Sorry for the meta discussion, but where I'm from "reflection" means functions for analysis, http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29.
This subject seems to be "Imperative equivalence for declarative loading" or similar).
caridy commentedon Mar 12, 2015
To clarify, or to raise the question, depending on how you look at this, but
local.import("./foo.js").then(def => ...)is really not reflective as it is the current proposal, essentially becausedefwill not get updated if the value change in the provider module.[-]reflection[/-][+]reflection: imperative equivalence for declarative import[/+]domenic commentedon Mar 12, 2015
Hmm maybe that is an argument for only having the namespace form.
guybedford commentedon Mar 12, 2015
importDefault?arv commentedon Mar 12, 2015
I'm not sure what problem we are trying to solve here?
How is
better than:
Lets try to keep the API surface area minimal. The special cases can always be done in user code and added later version of the spec.
matthewp commentedon Mar 12, 2015
Yes, I'm with @arv on this, es6 with make this elegant enough without the need for multiple methods/signatures.
loader.importshould return the module. import/export is already complicated enough (I still have to reference this all the time), let's not muddle the dynamic api as well.dherman commentedon Mar 12, 2015
@arv @matthewp A low-level reflection API can be minimal and general. But the local import object is a high level API, meant for common use. And it's meant to be the dynamic analog of what you can do with the declarative syntax. It should have its defaults be symmetric with the surface syntax.
In the surface syntax, when you import a default, you don't have to write d-e-f-a-u-l-t. It's just the simplest thing where you have to say the least. In short, we want to allow:
as the most convenient case just like
would be.
Another way of looking at this is that single-export needs to be as convenient in this API as it is today in existing JS module systems. Not having to say
defaultexplicitly is very important to the ergonomics.But we also have to allow importing the namespace object, of course. And sometimes a module will not have a default export, so forcing you to write
.then((dummy, named) => ...)is obnoxious for those cases. Hence a second method specifically for asking for the namespace object.dherman commentedon Mar 12, 2015
@caridy @domenic It's important to understand the use case of local import as not being about reflection but rather about dynamic loading. Reflection is a low-level operation meant for when you're trying to extend the base semantics of the language/system. Dynamic loading is simply a thing apps commonly need to do.
Also keep in mind that
.importwill not resolve the promise until the module has been initialized, fully executing its top-level code. The overwhelming majority of the time, the aliasing behavior is only important for the initialization phase where top-level bindings are being set up. It's extremely rare to mutate top-level exports after that point. So it's inappropriate to optimize this API around that as a constraint.Again, the low-level loader API should fully generalize the semantics, and there it is certainly sensible to have operations that just produce the namespace object as the most general thing. But the purpose of the local import API is as a high-level API whose ergonomics and style should mirror the ergonomics and style of the declarative syntax.
caridy commentedon Mar 12, 2015
Yes
Agree. We can probably make the case that the value of the
defaultexport should never change, in which case something likelocal.import("./foo.js").then(def => ...)should be fine.matthewp commentedon Mar 12, 2015
You have to say default all the time already, it's a common way to export things. I see your point about having symmetry between the forms. But the declarative form is nice because of syntactical affordances you won't have here.
Question,
even valid? I thought Promise only had a single return value.
dherman commentedon Mar 12, 2015
The important distinction is that you only say default inside the implementation of a module, just as you have to do something explicit in CommonJS/Node (
module.exports = ...;). But on the consumer side, you don't have to think about it having a name. Anyway sorry to beat a dead horse; it sounds like you agree the symmetry is important.LOLOLOL. (At least I'm slightly comforted by the fact that the lead of the promises spec didn't catch this either. ;-P)
I think a reasonable alternative is two forms, with the short name providing the default, and the longer name providing the namespace object:
The latter lets you extract the default export if you want:
Or you can combine the two operations:
caridy commentedon Mar 12, 2015
jejeje,
importAllresonates well with me considering the work we have done on https://github.com/estree/estree/blob/master/es6.md#exportalldeclaration, which is symmetrical to the import all concept. I like it.still we should clarify whether and how people will be able to update the default export from within a module, if there is no way to do that, then this syntax will be perfect.
johnjbarton commentedon Mar 12, 2015
I would appreciate a simplified explanation of what you all are talking about here. What is a "namespace object"?
44 remaining items
dherman commentedon Apr 2, 2015
But yes, it's contextual per-module.
matthewp commentedon Apr 2, 2015
That's interesting, I hadn't heard of this feature. I'll have to dive into esdiscuss then, but I think I understand what you're saying.
I like the idea overall and agree it's a clever way of working around this problem. You do still want access to the loader within a module,
import.localmaybe.dherman commentedon Apr 2, 2015
I should also show what this ends up looking like in
asyncfunctions:matthewp commentedon Apr 2, 2015
Very elegant!
MajorBreakfast commentedon Apr 3, 2015
This look pretty elegant!
Everything suddenly becomes very clean if we can go this way.
Don't be too frustrated: We can restart the naming discussion around this syntax! :) :)
You tell me if treating import as a function does even work.
I just assume that it's possible since the static syntax always includes theParsers are probably able to tell the two apart because the static import is never valid ES5 and this is except for the reserved keyword thing. Not sure if we want to go this way, but I'm proposing it anyway :)frompart, which should enable parsers to tell the two apart.bmeck commentedon Jul 10, 2015
Does this need to be a metaproperty and not some form of a function? It seems like a language extension would mean this loader would have to be the backer for all host environments if it assumes it gains meta-properties.
matthewp commentedon Jul 10, 2015
@bmeck Not having the Loader be the backer for all host environments would be a major mistake.
bmeck commentedon Jul 10, 2015
@matthewp you need to explain that in depth. loader vs es import/export are 2 different things to my knowledge
matthewp commentedon Jul 10, 2015
They are different things but users write code that run in multiple JavaScript environments.
bmeck commentedon Jul 10, 2015
@matthewp it has different implications semantically than existing code/systems (couchdb, webworkers, mongodb, node, ringojs, ...) which is the issue. having the semantics able to match would be nice as well as be much safer for upgrade purposes
matthewp commentedon Jul 10, 2015
I agree, a breaking change for those legacy systems is probably a no-go. I wish there were more participation here from all interested parties but this repo has been largely dark for the last several months.
There's a specification at amodro-lifecycle that attempts to address some of these legacy needs such as Node, I would suggest giving it a read if you have time.
bmeck commentedon Jul 10, 2015
@matthewp my last experience here did not include metaproperties or requirements for it to be used as the
importhook handler for VMs. WebAssembly/design#256 brought me here made me see several changes like this issue which make it a much larger scope than I thought it was. my interest came as a result of the increased scope and WASM making me come reread it in the current formmatthewp commentedon Jul 10, 2015
The metaproperty proposal only exists in this issue and hasn't been written to any spec AFAIK. There hasn't been any increase in scope to my knowledge, it is now much smaller is scope actually.
bmeck commentedon Jul 10, 2015
@matthewp we commented on it because it is a language extension which immediately concerned us. A lot of work is often done in conversations and only shows up in specs to replace TODOs etc. at a very late stage if we want to read public available text. I think talking in issues is fairly similar to talking about the spec.
caridy commentedon Jul 10, 2015
@matthewp @bmeck just a side note, the
importimperative call is bound to the loader instance that loaded the host module in the first place (just likerequire()is bound to the host module), which means all modules will have access to it since they all have a loader associated, even if it is the built-in default loader implemented by the runtime.