josevalim

josevalim

Creator of Elixir

Call to library authors: define "Nutrition facts" for your meta-programming

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

First 10 of 21 Posts Switch mode

josevalim

josevalim OP

Creator of Elixir
kip

kip

ex_cldr Core Team

Good idea. I’ll update the ex_cldr libs over the next week or so.

Update: added to the ex_cldr README and the Cldr module and published to hex.pm.

18
Post #2
Nicd

Nicd

This is where libraries that don’t use use win, nothing to document! :grin:

17
Post #3
sergio

sergio

Best code is no code! :joy:

sodapopcan

sodapopcan

With careful consideration, use beneficially cleans up a lot of noise and I’m glad it exists.

(•_•) even if it is a bit
( ••)>⌐■-■
(⌐■
■) overused

Sorry, I couldn’t resist. I’ll see myself out.

33
Post #5
aiwaiwa

aiwaiwa

In this case, it feels like Kernel — Elixir v1.20.2 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.

christhekeele

christhekeele

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.

aiwaiwa

aiwaiwa

Right, but hovering over :controller in Phoenix

use ProjectWeb, :controller

could’ve unwrapped what’s a bunch of stuff is delivered to you by it.

  • Hover over use shows macro’s hint.
  • Hover over ProjectWeb provides nutrition facts.
  • Hover over :controller gives a clue of what is being brought in.
fceruti

fceruti

On a somewhat related note, a cool feature available on neovim using elixir-tools, is the expand macro:

162372669-4782baba-1889-4145-8a4f-e3bf13a6450d

18
Post #9
aiwaiwa

aiwaiwa

Thank you! It’s a great feature! The hint is you need to select for it to actually work, I thought it was broken!

And it duplicates the output on my end too in VSCode.

Where Next?

Trending in Notices Top

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement