Skip to content

reflection: imperative equivalence for declarative import #36

Open

Description

@caridy

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).

@dherman suggested:

local.import("./foo.js").then((def, named) => ...)
local.importNamespace("./foo.js").then(m => ...)

Activity

  1. domenic commented on Mar 12, 2015

    @domenic
    Member

    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))?

  2. johnjbarton commented on Mar 12, 2015

    @johnjbarton

    (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).

  3. caridy commented on Mar 12, 2015

    @caridy
    ContributorAuthor

    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.

  4. changed the title [-]reflection[/-] [+]reflection: imperative equivalence for declarative import[/+] on Mar 12, 2015
  5. domenic commented on Mar 12, 2015

    @domenic
    Member

    Hmm maybe that is an argument for only having the namespace form.

  6. guybedford commented on Mar 12, 2015

    @guybedford

    importDefault?

  7. arv commented on Mar 12, 2015

    @arv
    Contributor

    I'm not sure what problem we are trying to solve here?

    How is

    local.import("./foo.js").then((def, named) => ...)

    better than:

    local.import("./foo.js").then({default: def} => ...)
    local.import("./foo.js").then({foo} => ...)

    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.

  8. matthewp commented on Mar 12, 2015

    @matthewp

    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.

  9. dherman commented on Mar 12, 2015

    @dherman

    @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:

    local.import("./my-app-controller.js").then(MyAppController => ...)

    as the most convenient case just like

    import MyAppController from "./my-app-controller.js";

    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 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.

  10. dherman commented on Mar 12, 2015

    @dherman

    @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.

  11. caridy commented on Mar 12, 2015

    @caridy
    ContributorAuthor

    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.

  12. matthewp commented on Mar 12, 2015

    @matthewp

    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.

  13. dherman commented on Mar 12, 2015

    @dherman

    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:

    l.import("./rubber-chicken.js").then(RubberChicken => ...)
    l.importAll("./utils.js").then(utils => ...)

    The latter lets you extract the default export if you want:

    l.importAll("jQuery").then(namespace => {
      let $ = namespace.default;
      ...
    });

    Or you can combine the two operations:

    Promise.all(l.import("jQuery"), l.importAll("jQuery"))
      .then(([$, namespace]) => ...);
  14. caridy commented on Mar 12, 2015

    @caridy
    ContributorAuthor

    jejeje, importAll resonates 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.

  15. johnjbarton commented on Mar 12, 2015

    @johnjbarton

    I think a reasonable alternative is two forms, with the short name providing the default, and the longer name providing the namespace object:

    l.import("./rubber-chicken.js").then(RubberChicken => ...)
    l.importAll("./utils.js").then(utils => ...)
    

    I would appreciate a simplified explanation of what you all are talking about here. What is a "namespace object"?

  16. 44 remaining items

  17. dherman commented on Apr 2, 2015

    @dherman

    But yes, it's contextual per-module.

  18. matthewp commented on Apr 2, 2015

    @matthewp

    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.

  19. dherman commented on Apr 2, 2015

    @dherman

    I should also show what this ends up looking like in async functions:

    let Spinner = await import.default("Spinner");
    let { readFile, writeFile } = await import.namespace("fs");
    ...
  20. matthewp commented on Apr 2, 2015

    @matthewp

    Very elegant!

  21. MajorBreakfast commented on Apr 3, 2015

    @MajorBreakfast

    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 function
    
    import.namespace('fs').then(fs => ...) // Namespace import
    
    import('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 :)

  22. bmeck commented on Jul 10, 2015

    @bmeck

    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.

  23. matthewp commented on Jul 10, 2015

    @matthewp

    @bmeck Not having the Loader be the backer for all host environments would be a major mistake.

  24. bmeck commented on Jul 10, 2015

    @bmeck

    @matthewp you need to explain that in depth. loader vs es import/export are 2 different things to my knowledge

  25. matthewp commented on Jul 10, 2015

    @matthewp

    They are different things but users write code that run in multiple JavaScript environments.

  26. bmeck commented on Jul 10, 2015

    @bmeck

    @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

  27. matthewp commented on Jul 10, 2015

    @matthewp

    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.

  28. bmeck commented on Jul 10, 2015

    @bmeck

    @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

  29. matthewp commented on Jul 10, 2015

    @matthewp

    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.

  30. bmeck commented on Jul 10, 2015

    @bmeck

    @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.

  31. caridy commented on Jul 10, 2015

    @caridy
    ContributorAuthor

    @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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions