nikody

nikody

Why does the Elixir compiler show a warning for a variable in a list?

This is not really a problem as much as something that I found a bit baffling.

I found code similar to the following in an ExUnit test as a part of a Phoenix application:

strawberry = strawberry_fixture()   # creates a strawberry
assert [strawberry] = Garden.list_strawberries()

The interesting thing for me is that the Elixir compiler ocassionally displays a warning that the strawberry variable is unused. Does anyone know why that happens?

Most Liked

mindriot

mindriot

You have clearly found the solution to your actual problem but in an attempt to answer your question…

The “=” sign is a pattern matching operator, which as you allude to is not the same as equality comparison. The “strawberry” is being bound to a value in the fixture setup and then re-bound in the assertion through the pattern match but it is never read. The compiler warning is very useful in catching mistakes where you think you are matching but you actually are not. You can, as you found, fix this by changing to comparison. However if you do want to pattern match you can pin the value of “strawberry” in the pattern match so that it cannot be rebound using the pin operator “^” like so:

strawberry = strawberry_fixture()   # creates a strawberry
assert [^strawberry] = Garden.list_strawberries()

The pin operator can be used in any case where you want to do a pattern match that asserts the variables value be used as part of the match rather than allowing the variable name to be re-bound.

mindriot

mindriot

The one on the first line is never read due to being clobbered on the second line. I’m not entirely sure exactly how it works deep down; my assumption is that on the second line the assert may use it or do enough to make the compiler think it is being used otherwise I would personally think two warnings would make more sense.

nikody

nikody

I hadn’t noticed that the assert had only one equals sign. Using == seems to solve this issue.

nikody

nikody

Thank you for the response. :slight_smile: It does seem to be due to the pattern matching but the warning was always about the variable on the first line. I assumed that it should be for the variable that gets assigned inside the list, but it was not the case. It is for sure due to the pattern matching though.

I always forget about the pin operator apart from when using Ecto queries. Thank you for reminding me. :slight_smile:

Where Next?

Popular in Questions Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
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
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
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
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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

Other popular topics Top

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
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
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
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
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 45766 226
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
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
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement