-
Notifications
You must be signed in to change notification settings - Fork 469
RFC: single-context Universal Libraries #10630
Copy link
Copy link
Open
Labels
melangeMelange rules and generatorMelange rules and generatorproposalRFC's that are awaiting discussion to be accepted or rejectedRFC's that are awaiting discussion to be accepted or rejected
Description
Context
(using melange 0.1)allows writing Melange / OCaml code together with limitations- Recent work on "universal libraries" proposes using different contexts and
enabled_if
Problems
- Multi-context libraries introduce a few problems:
librarystanzas end up duplicated for any non-trivial use cases, and(enabled_if (= %{context} melange))is infectious (melange libraries only work within themelangecontext)- it's not trivial to install multi-context libraries in the same context as native (for consumption within the same OPAM switch).
- Editor support becomes more painful, and switching between files in different contexts still requires restarting the LSP.
Requirements:
- Installing multi-mode libraries alongside each other
- vary module sources according to the library
mode - vary preprocessing according to the library
mode - vary library dependencies according to the library
mode
Proposed solution
- The proposed solution is 2-fold:
- new conditional fields
libraries,preprocess,flags,modulesin(library ..)- prior art:
melange.compile_flags - in their absence, a library with
(modes melange)falls back to the fields already supported before this change.
- prior art:
- the ability to choose module sources based on the mode e.g. foo.%{mode}.ml
- we can solve the "
copy_filesproblem" by having shared module sources and mode-specific sources in the same directory, with different extensions. Example:
- we can solve the "
- new conditional fields
- src
|_ dune
|_ common.ml
|_ foo.melange.ml
|_ foo.ocaml.ml
Slightly unrelated: this proposal is likely backwards-compatible with (using melange 0.1); after adopting it successfully in universal library projects such as Ahrefs, I propose we mark the Melange extensions stable in dune (1.0).
Implementation details
- we can keep module sources in the same directories in the build folder
- for Melange, we'll have to copy them to e.g. a
.melange-sourcesfolder after setting up rules for the implementation-specific module sources - Installation keeps working since we already copy melange artifacts to
<lib-root>/<pkg-name>/melange. We can install the Melange sources there, too.
Caveats / further work
It's likely we'll have to change Merlin to map a module name to its source file. This is desirable in general even without this proposal: Merlin currently guesses what module source corresponds to a module, and implementing this mapping would be a benefit for most OCaml projects and editor tooling.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
melangeMelange rules and generatorMelange rules and generatorproposalRFC's that are awaiting discussion to be accepted or rejectedRFC's that are awaiting discussion to be accepted or rejected