Using module attributes in typespec definitions to reduce duplication?

Hi

I’m trying to use module attributes in my typespec, but I get errors when I do this:

defmodule KitchenCalculator do
  @ml :milliliter
  @cup :cup
  @fluid_ounce :fluid_ounce
  @tsp :teaspoon
  @tbsp :tablespoon

  @type unit :: @ml | @cup | @fluid_ounce | @tsp | @tbsp

  # More stuff here
end

I get the error:

** (CompileError) lib/kitchen_calculator.ex:8: type ml/0 undefined (no such type in KitchenCalculator)
    (elixir 1.12.2) lib/kernel/typespec.ex:925: Kernel.Typespec.compile_error/2
    (stdlib 3.15.2) lists.erl:1358: :lists.mapfoldl/3
    (elixir 1.12.2) lib/kernel/typespec.ex:834: Kernel.Typespec.typespec/4
    (stdlib 3.15.2) lists.erl:1358: :lists.mapfoldl/3
    (elixir 1.12.2) lib/kernel/typespec.ex:464: Kernel.Typespec.typespec/4
    (elixir 1.12.2) lib/kernel/typespec.ex:307: Kernel.Typespec.translate_type/2
    (stdlib 3.15.2) lists.erl:1358: :lists.mapfoldl/3
    (elixir 1.12.2) lib/kernel/typespec.ex:235: Kernel.Typespec.translate_typespecs_for_module/2

I use the module attributes later on, to reduce duplication and chance of errors via typos when typing out atoms. Is there any way to achieve this, or do I just live with the minor duplication?

1 Like

You can do it like this:

defmodule KitchenCalculator do
  @ml :milliliter
  @cup :cup
  @fluid_ounce :fluid_ounce
  @tsp :teaspoon
  @tbsp :tablespoon

  types = Enum.reduce([@ml, @cup, @fluid_ounce, @tsp, @tbsp], &({:|, [], [&1, &2]}))
  @type unit :: unquote(types)

  # More stuff here
end

You could of course encapsulate the Enum.reduce/2 into a macro and reuse it that way.

iex> t KitchenCalculator.unit
@type unit() :: :tablespoon | :teaspoon | :fluid_ounce | :cup | :milliliter

BTW, if you’re spending a lot of time with units, unit math and unit conversions, and potentially unit serialisation and localization, you might find ex_cldr_units helpful (I’m the author).

4 Likes

Thanks @kip . Need to get into macros, an as yet undiscovered part of Elixir for me :slight_smile:

In the few occasions when I’ve used the technique above I have done it directly. In my cases, building a macro just to do that hasn’t been important and I felt it would actually make the code more obscure. By having the Metaprogramming line directly above, it makes it easy for “future me” to work out what I was thinking.

I’m sure you’ve heard the aphorism in Elixir-land, “first rule of macros is don’t use macros”.

3 Likes