One common concern about using Elixir libraries is that use SomeModule does not make clear how it impacts the caller. While the goal of use is precisely to provide a common extension point, and most of the time those changes are minor, it is impossible to know what it does without reading the source code.
For this reason, from Elixir v1.15, we are recommending libraries to include a summary alongside each module, typically within the first section of its @moduledoc, that succinctly explains the impact of using said modules. In this pull request, we have added this note to all use XYZ modules in stdlib, like this:
> #### `use GenServer` {: .info}
>
> When you `use GenServer`, the GenServer module will
> set `@behaviour GenServer` and define a `child_spec/1`
> function, so your module can be used as a child
> in a supervision tree.
Which will be rendered like this (see block at the bottom):
Think of it as āNutrition factsā for meta-programming. If you are a library author and your modules can be used, please consider doing the same.
In this case, it feels like Kernel ā Elixir v1.15.0-rc.1 itself could say a bit less obscure about the use itself.
Right now it says Uses the given module in the current context. as a first sentence. It gives more room for questions like āwhat is context?ā. āwhat does uses mean?ā instead of hinting that there will be something that gets generated along with importing.
In fact hovering over use in VSCode is what I do when trying to get a refresher on what it actually performs.
I think itād be helpful for IDEs to replace the hinttext for such things to display the @doc of the __using__ macro for the module itself, rather than the general docs for use and the Module respectively. If the library has nutrition facts set up, thatād make it a really clean development experience!
Oh never mind, I see that the nutrition facts in stdlib are in the @moduledoc not the macro @doc, so the current experience is ok.
Before PRing though. What do you think? Imagine yourself a few months Elixir user!
use(module, opts \\ [])
Applies a module as a compile-time extension to the current context.
The use macro combines a require of the module with the invocation of its __using__/1 function, allowing the module to define macros, callbacks, or other compile-time constructs.
This is going to be quite similar, but thatās not something that can be easily avoided.
What I would love to see, and maybe there is such thing already, is an information given to me back when I āuse SomeModuleā in my editor/IDE. I guess thatād be the languageās server doing, that would report to me, in a pop-up or some other form, what happened here, which behavior(s) was introduced, what functions were defined, what modules got imported, things like that.
The documentation is static, and has to be maintained, the language server is dynamic and can generate an actual report, that would take into consideration options pased to the āuseā macro, to give me a precise overview of what actually happened to my code when I used it.
The same thing could be available for other macros, also āimportā to know what functions got imported.
Again, this may already be available in full or partially, Iām not an expert on the subject.
EDIT: Oh hi there itās me again commenting on a thread without reading it fully. Need to try this, not in language server and NeoVim specific but looks like what I suggested: