[Proposal] import only: :guards

44 views
Skip to first unread message

Christopher Keele

unread,
Oct 22, 2023, 1:27:21 AM10/22/23
to elixir-lang-core
Currently, import <module>, only: <selector> accepts as a selector either:
  • a keyword list of functions/arities
  • :functions to import only all functions
  • :macros to import only all macros
Additionally, the selector can be :sigils, which only imports a specific subset macros: those known to be sigils (based on the naming convention of defmacro sigil_...).

I propose extending the selector to allow :guards, which would only import macros from the <module> labelled as @doc guard: true (including, automatically, macros defined via defguard).

Similar to the __using__ and sigil__... macros, guards are a commonly-desirable-to-import subset of a module's macros. Additionally, this would reduce the cognitive dissonance between the fact that a macro defined as a defguard is not transparently a defmacro under the covers.

In programming, the hardest problems involve going from the 0 to 1 case, and the 1 to 2 case, after which hopefully you've solved for N + 1 cases. As an additional proposal, I propose supporting atom lists as a selector in import <module>, only: <selector>. This would elegantly allow for the common case of only importing high-value unqualified macros; namely:  import <module>, only: [:sigils, :guards] without conflicting with the functions/arities keyword selector format. Mixing a :macro selector with either :sigils or :guards in an atom selector list would issue a compiler warning about the redundancy.

As former proposal handles my use-case, and the latter is a natural but not-required extension to the feature, I leave it for debate if a mixed atoms/keywords should be supported in the additional proposal: namely, something like import <module>, only: [:guards, sigil_URL]

Christopher Keele

unread,
Oct 22, 2023, 1:31:27 AM10/22/23
to elixir-lang-core
Sent early, oops.

I leave it for debate if a mixed atoms/keywords should be supported in the additional proposal: namely, how something like import <module>, only: [:guards, sigil_URL: 2] would behave, and warn on overlap, for example if [:sigils, sigil_URL: 2] was provided as a selector.

José Valim

unread,
Oct 31, 2023, 12:33:54 PM10/31/23
to elixir-lang-core
Unfortunately we should not rely on the Docs chunk, because it is not expected to be available. We could add infrastructure to this but I would say only: :macros give a good enough approximation (and they'd cover patterns and guards).

Christopher Keele

unread,
Oct 31, 2023, 2:56:47 PM10/31/23
to elixir-l...@googlegroups.com

> only: :macros give a good enough approximation

If you're cozy with the current situation, I am too. I reached for this proposal when I typed it out, convinced it already existed—thought I'd give it the rounds and see if it resonated!

--
You received this message because you are subscribed to a topic in the Google Groups "elixir-lang-core" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-lang-core/Q_HBkDFwdHw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/623c24aa-6201-4f86-b5f3-993fbe576fb7n%40googlegroups.com.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages