Bitstring specifier macros

I’ve just come across a library that had import Foo.BinaryUtils, warn: false peppered through it. That looked like a code smell so I started investigating. It turned out this module had macros in it used for bitstring specifiers, e.g.:

  defmacro uint do
    quote do: little - unsigned - 16

  defmacro udint do
    quote do: little - unsigned - 32

And these are used like so:

<<foo::uint(), bar::udint(), rest::binary>>

My question is why does this not work with require? Shouldn’t it? The library authors used warn: false because the module only contains these macros, and elixirc complains that the import is not used otherwise.

Turns out I was misunderstanding how require and import work. What this library should have been doing is import Foo.BinaryUtils, only: :macros, which communicates much more clearly the intent.

1 Like

Elixir will not complain if you don’t not use all the macros from an imported module. It may be complaining because you are not using any at all.

Feel free to share some code, either gist or a repo and I can have a look at it and give you a more informed answer.

It’s definitely using the macros. Taking out the import prevents compilation, and the error shows where the macro is used. And I just found out that the warning still happens if I replace warn: false with only: :macros. The repo is Scrub, but I didn’t write it.

Where is the specific line of code I should look at?

Lots of examples, but lib/scrub/session/protocol.ex:28 and following is a good one to look at.

Gah, never mind. It’s the lib/scrub.ex where the warning happens, but that module doesn’t actually use the macros. Everything works as intended.