DenisGorbachev

DenisGorbachev

Is there a way to suppress warnings about unused variables?

Our tests are structured like this:

response = say("Place order")
response = say("BITTREX 1 LTC 0.0116 BTC")
response = say("0.0109")
response = say("0.0159")

When we run them, we get a lot of warning: variable "response" is unused. Of course, we could just add underscore, but it’s inconvenient - sometimes “response” variable is actually used in assertion.

Is there a way to suppress the warning: variable "response" is unused in a general way, without underscoring its name?

Marked As Solved

DenisGorbachev

DenisGorbachev

To answer my own question:

defmodule ModuleName do
  @compile :nowarn_unused_vars
end

Also Liked

NobbZ

NobbZ

I do still hope though, that you reconsider your current API to something more explicit and less side-efficty despite the compiler-switch…

NobbZ

NobbZ

Why in a variable at all?

response = say("foo")
assert "sayed foo" = response

This snippet will totally obfuscate your assertion when ExUnit prints it in the failure case. Instead of how the actual value was produced you’ll only get printed response didn’t match, where by using the function call directly you will be told that say("foo") didn’t match the expectation, which in my opinion is much more obvious.

Besides of that, assuming purity, it does not make sense to what you did there. Either you care for the returned value and assert against it, or you are only interested in the side effect which you then mark explicitely with an underscore(d name), or you are interested in both, this is usually when assert against the return value first and then against the expected effects.

Qqwy

Qqwy

TypeCheck Core Team

Why is it inconvenient to replace response with _ or _response in the cases where it is not used? It would make your code easier to read and understand for other developers.

NobbZ

NobbZ

In this case, I’d really rethink the API.

You seem to modify a global, invisible something with each call. When you do this from multiple processes this will probably be a big shoot in your own foot.

I really think its better than to do it like this:

response = init_say()
|> say("a")
|> say("b")

assert_text response, "a, b"

Or even:

init_say()
|> say("a")
|> say("b")
|> assert_text("a, b")

Building the value this way is much more “thread”-safe and less likely to “race”.

OvermindDL1

OvermindDL1

iex(2)> defmodule Blah do
...(2)>   def bloop, do: a = 42
...(2)> end
warning: variable "a" is unused
  iex:3

{:module, Blah,
 <<70, 79, 82, 49, 0, 0, 3, 124, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 83,
   0, 0, 0, 8, 11, 69, 108, 105, 120, 105, 114, 46, 66, 108, 97, 104, 8, 95, 95,
   105, 110, 102, 111, 95, 95, 9, 102, ...>>, {:bloop, 0}}
iex(3)> defmodule Blahs do     
...(3)>   def bloop, do: unquote(quote generated: true, do: a) = 42
...(3)> end
{:module, Blahs,
 <<70, 79, 82, 49, 0, 0, 3, 148, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 84,
   0, 0, 0, 8, 12, 69, 108, 105, 120, 105, 114, 46, 66, 108, 97, 104, 115, 8,
   95, 95, 105, 110, 102, 111, 95, 95, 9, ...>>, {:bloop, 0}}

You could always wrap it up in a macro too quite easily so you could do something like ignore_warnings(a) = 42 or so.

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
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
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
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
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

We're in Beta

About us Mission Statement