sfrances

sfrances

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?

Marked As Solved

kip

kip

ex_cldr Core Team

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

Also Liked

kip

kip

ex_cldr Core Team

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

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49266 226
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement