Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Automatic longer namespacing and aliasing at import time. #2649

Open
jordwalke opened this issue Sep 18, 2019 · 3 comments
Open

Proposal: Automatic longer namespacing and aliasing at import time. #2649

jordwalke opened this issue Sep 18, 2019 · 3 comments

Comments

@jordwalke
Copy link
Contributor

@jordwalke jordwalke commented Sep 18, 2019

Proposal: Make it convenient for module "namespaces" to be prefixed with an upper camel case of their package name automatically, similar to how their public name (should be) is by convention. Then allow importing them as shorthand:

Motivation: Make it easy for library authors to avoid namespace collisions at link time, but also give them a convenient/standard way to shorten things at consumption time.

Let's suppose there are two packages loosely referred to as "react" (I'm sorry). One might be in the opam package react, and another might be in a package called react-ui - and the two serve different purposes. You might want to mix them in the same project.

Proposal:
react-ui/react/dune

(library
  (public_name react-ui.react)
)

react/dune

(library
  (public_name react)
)

Could be shorthand for:

react-ui/react/dune

(library
  (name ReactUi__React)
  (public_name react-ui.react)
)

react/dune

(library
  (name React)
  (public_name react)
)

Then the second part of the proposal, is the ability to alias modules at consumption time:

myApp/dune

(library
  (name MyApp)
  (libraries (React react-ui.react))
)

yourApp/dune

(library
  (name MyApp)
  (libraries (React react))
)

When developing myApp, it will see the module React as an alias to the react-ui.react library. yourApp will see React as an alias to the react (non-UI) library.

This also provides a way to let people resolve any ambiguity if there are two "conceptual Reacts" in the same app:

ourApp/dune

(library
  (name OurApp)
  (libraries (ReactUi react-ui.react)  (React react))
)

What this would do is it would create a hidden module called OurApp_openable.ml which does:

module ReactUi = ReactUi__React
module React = React

And then pass the -open OurApp_openable flag when compiling the application. I believe this openable module should be compiled with module aliases, but I'm not certain that is required.

@diml

This comment has been minimized.

Copy link
Member

@diml diml commented Sep 18, 2019

We should discuss the two proposal separately:

  • automatically derive the name from public_name, even when public_name is not a valid OCaml module name
  • allow to rename dependencies

Could you open two different tickets so that it's easier to discuss them independently?

@jordwalke

This comment has been minimized.

Copy link
Contributor Author

@jordwalke jordwalke commented Sep 18, 2019

Do you think that the two should be considered separate proposals? I broke off the other independent feature #2648 because I believe it can be considered independently, but for this one, I'm not sure if it's possible to implement one feature without the other (automatically derive name, and shortening on import). The reason is that I think the best automatically derived name would be ugly PkgPrefix__NamespaceName, and would strongly benefit from having a shortening convention upon requiring. Happy to break it apart if you still feel they should be independent.

@diml

This comment has been minimized.

Copy link
Member

@diml diml commented Sep 19, 2019

Yes. I agree that the automatic name derivation would be less practical without the aliasing of dependencies, but the former would be useful on its own. And at the end of the day, technically none depend on the other and the set of questions to consider is disjoint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.