warren84

warren84

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

I would like to know what I can do to get rid of the “incompatible types” warnings that are cluttering my phoenix server logs - I would willingly accept “operator error” as the cause if I could learn how to prevent the warnings. This is essentially the same as Destructuring a 2-tuple throws linter warning but still works. Incompatible types: tuple() !~ {var1, var2} - I am also using vscode and ElixirLS - in that post, one comment said that the problem would be fixed elixir 1.13 - I’m using 1.15. The phoenix app fine despite the warnings in the logs

Any help/direction is appreciated.

++++++++++++++++++++++++++
warning: incompatible types:

tuple() !~ {var1, var2, var3}

in expression:

# lib/dancePHX/dance_parser.ex:280
{ol, bts, d_bts} = acc

where “acc” was given the type tuple() in:

# lib/dancePHX/dance_parser.ex:278
elem(acc, 1)

where “acc” was given the type {var1, var2, var3} in:

# lib/dancePHX/dance_parser.ex:280
{ol, bts, d_bts} = acc

++++++++++++++++++++++++++
the code that’s generating this warning:

def _gen_phrases(el, acc) when elem(acc, 1) < 16 do <<<— line 278
[fig, b, _c] = el
{ol, bts, d_bts} = acc

...

end

There are also _gen_phrases(el, acc) functions using elem(acc, 1) == 16 and elem(acc, 1) > 16 which generate the same warning.

I’m using phoenix 1.6.11 and elixir 1.15.6.

++++++++++++++++++++++++++
$ elixir -v
Erlang/OTP 26 [erts-14.1] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit:ns]
Elixir 1.15.6 (compiled with Erlang/OTP 26)

Most Liked

dimitarvp

dimitarvp

Well, let me just say that I had no clue that function names could be prefixed with underscores.

03juan

03juan

I assume it’s warning that the basic tuple() type denotes tuples of any size and you’re matching it to a 3-element tuple, where it could fail.

A tuple in a pattern will match only tuples of the same size
Patterns and Guards — Elixir v1.15.6

You could explicitly match the correct shape in the function definitions:

def _gen_phrases(el, {ol, bts, d_bts} = acc) when bts == 16
# or alternatively 
# def _gen_phrases(el, {ol, bts = 16, d_bts} = acc)

def _gen_phrases(el, {ol, bts, d_bts} = acc) when bts < 16

def _gen_phrases(el, {ol, bts, d_bts} = acc) when bts > 16
# or leave out the guard as the other 2 clauses match everything <=16
christhekeele

christhekeele

Yep, they’re conventionally considered to be “hidden” (similar to double-underscore-prefixed struct fields) and will never be implicitly imported, auto-displayed in exdoc, or listed in iex helpers.

03juan

03juan

It was under our noses all along…

Function names may also start with an underscore. Such functions are never imported by default…
Naming conventions — Elixir v1.16.3

It’s one of those things that we kinda learn about by practice and intuition, like every module’s __info__/1 or the venerable __MODULE__/0 and __ENV__/0, that we could actually apply to our own code but rarely do.

sodapopcan

sodapopcan

Even more to the point __using__/1, __before_compile__/1, __after_compile__/2, etc!

EDIT: wait, no not more to the point! TIL __MODULE__/0 is a function :sweat_smile:

EDIT 2: wait… is it? I’m just going to shut up now, lol.

Where Next?

Popular in Questions Top

Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
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
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
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
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
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
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
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 th...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

We're in Beta

About us Mission Statement