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
Correct. This will be fixed in v1.13.
Also Liked
kip
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
What is this linter you are using?
(Whatever it is, it just states, that not all tuples are 2-tuples.)
Qqwy
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







