aegatlin

aegatlin

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

Showing Posts 1 to 5

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
aegatlin

aegatlin OP

Interestingly, the linter shows the warning even with a guard clause of ... when is_tuple(x) and tuple_size(x) == 2.

The linter is elixir-ls via the vscode wrapper for it. The method-level/case-level destructing does not produce the linter warning, which is why I marked the problem as solved (thank you all for your feedback :D). But, it still does seem a curious warning.

josevalim

josevalim

Creator of Elixir

Correct. This will be fixed in v1.13.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews