aegatlin

aegatlin

Destructuring a 2-tuple throws linter warning but still works. Incompatible types: tuple() !~ {var1, var2}

I am trying to destructure a tuple in a case statement and I am getting a warning that confuses me. It passes my test case, so it works, but the linter is giving me a warning and I’m not sure how to handle it. Am I doing a bad practice? I have a simplified scenario that demonstrates the warning below.

def test(x) do
  case x do
    tuple when is_tuple(tuple) ->
      {v1, v2} = tuple
      IO.puts(v1)
      IO.puts(v2)
  end
end

The warning is

incompatible types:
    tuple() !~ {var1, var2}
in expression:
    # [file path]
    {v1, v2} = x
where "x" was given the type tuple() in:
    # [file path]
    is_tuple(x)
where "x" was given the type {var1, var2} in:
    # [file path]
    {v1, v2} = x

Marked As Solved

josevalim

josevalim

Creator of Elixir

Correct. This will be fixed in v1.13.

Also Liked

kip

kip

ex_cldr Core Team

I don’t know the source of the warning, but it would be more idiomatic to remember that case clauses are match expressions so you can destructure more simply:

def test(x) do
  case x do
    {v1, v2} ->
      IO.puts(v1)
      IO.puts(v2)
  end
end
Sebb

Sebb

What is this linter you are using?
(Whatever it is, it just states, that not all tuples are 2-tuples.)

Qqwy

Qqwy

TypeCheck Core Team

And if the only input you ever expect in your function is an arity-2 tuple, then you can also pattern-match in the function clause itself:

def test({v1, v2}) do
  IO.puts(v1)
  IO.puts(v2)
end

Where Next?

Popular in Questions Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
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 Postgres...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
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
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New

Other popular topics Top

mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43757 214
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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 48342 226
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement