tomekowal

tomekowal

How to fix type violation warning from a macro in Elixir 1.18

I have a macro that is used for parsing:

  defmacro left <~> right do
    quote do
      case unquote(left) do
        :error ->
          unquote(right)

        {:error, _} ->
          unquote(right)

        {:ok, _} = r ->
          r

        {:ok, _, _} = r ->
          r
      end
    end
  end

It is finding first thing that evaluates to {:ok, _} or {:ok, _, _} in a chain like parse1(args) <~> parse2(args) <~> parse3(args). We are using a macro because it is lazy. E.g. we only evaluate parse2(args) when parse1(args) failed.

In Elixir 1.18, I started getting warnings like this:

    warning: the following clause will never match:

        {:error, _}

    because it attempts to match on the result of:

        parse(args)

    which has type:

        dynamic(:error or {:ok, term()})

which is correct. This particular parser return only :error or {:ok, term()}, but other usages of this macro use othe paths including {error, _} and {:ok, _, _}.

So, the warning is technically correct, but doesn’t play well with the macro. How could I solve that issue?

Marked As Solved

lud

lud

Hello,

In this case you could normalize the return types so errors are always a tuple.

Also Liked

LostKobrakai

LostKobrakai

Until there’s manual type notations you’d need to work around that, like discussed e.g. in

Where Next?

Popular in Questions Top

logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
Kagamiiiii
Student &amp; New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
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
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

We're in Beta

About us Mission Statement