# Bitstring specifier macros

**URL:** <https://forum.elixirforum.com/t/bitstring-specifier-macros/44542>\
**Category:** Questions\
**Tags:** macros, bitstring\
**Created:** [December 14, 2021, 6:31pm UTC](https://forum.elixirforum.com/t/bitstring-specifier-macros/44542 "2021-12-14T18:31:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![IslandUsurper](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/islandusurper/32/25161_2.png) [@IslandUsurper](https://forum.elixirforum.com/u/IslandUsurper)\
**Post date:** [December 14, 2021, 6:31pm UTC](https://forum.elixirforum.com/t/bitstring-specifier-macros/44542/1 "2021-12-14T18:31:25Z")

</div>

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

```elixir
  defmacro uint do
    quote do: little - unsigned - 16
  end

  defmacro udint do
    quote do: little - unsigned - 32
  end

```

And these are used like so:

```elixir
<<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.

---

<div class="post-metadata">

**Author:** ![IslandUsurper](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/islandusurper/32/25161_2.png) [@IslandUsurper](https://forum.elixirforum.com/u/IslandUsurper)\
**Post date:** [December 14, 2021, 7:09pm UTC](https://forum.elixirforum.com/t/bitstring-specifier-macros/44542/2 "2021-12-14T19:09:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![eksperimental](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/eksperimental/32/1401_2.png) [@eksperimental](https://forum.elixirforum.com/u/eksperimental)\
**Post date:** [December 14, 2021, 7:29pm UTC](https://forum.elixirforum.com/t/bitstring-specifier-macros/44542/3 "2021-12-14T19:29:21Z")

</div>

> [@IslandUsurper](#):
>
> `import Foo.BinaryUtils, warn: false`

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.

---

<div class="post-metadata">

**Author:** ![IslandUsurper](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/islandusurper/32/25161_2.png) [@IslandUsurper](https://forum.elixirforum.com/u/IslandUsurper)\
**Post date:** [December 14, 2021, 7:59pm UTC](https://forum.elixirforum.com/t/bitstring-specifier-macros/44542/4 "2021-12-14T19:59:10Z")

</div>

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](https://github.com/verypossible/scrub), but I didn’t write it.

---

<div class="post-metadata">

**Author:** ![eksperimental](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/eksperimental/32/1401_2.png) [@eksperimental](https://forum.elixirforum.com/u/eksperimental)\
**Post date:** [December 14, 2021, 8:01pm UTC](https://forum.elixirforum.com/t/bitstring-specifier-macros/44542/5 "2021-12-14T20:01:18Z")

</div>

> [@IslandUsurper](#):
>
> The repo is [Scrub](https://github.com/verypossible/scrub), but I didn’t write it.

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

---

<div class="post-metadata">

**Author:** ![IslandUsurper](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/islandusurper/32/25161_2.png) [@IslandUsurper](https://forum.elixirforum.com/u/IslandUsurper)\
**Post date:** [December 14, 2021, 8:02pm UTC](https://forum.elixirforum.com/t/bitstring-specifier-macros/44542/6 "2021-12-14T20:02:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![IslandUsurper](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/islandusurper/32/25161_2.png) [@IslandUsurper](https://forum.elixirforum.com/u/IslandUsurper)\
**Post date:** [December 14, 2021, 8:04pm UTC](https://forum.elixirforum.com/t/bitstring-specifier-macros/44542/7 "2021-12-14T20:04:54Z")

</div>

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.
