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?
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).
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”.