TomHale

TomHale

Why assign if/else to a variable?

In Elixir Succinctly I read:

Since everything in Elixir is an expression, and if is no exception, the if…else construct returns a value that we can assign to a variable for later use:

a = if test_conditional do
# ...

What are some practical usages of this?

Most Liked

NobbZ

NobbZ

You can have the value of a variable depending on some condition.

In other languages you see code like the following which would not work in Elixir due to elixirs scoping rules:

a = 2
if condition then:
    a = 1
pavancse17

pavancse17

if suppose you want to change any variable value inside if block in Elixir. You need to get back and re-assign the value to the same variable.

This is the code you will expect to work. But it won’t.

iex(7)> a = 5
5
iex(8)> is_odd = nil
nil
iex(9)> if rem(a, 2) == 0 do
...(9)>   is_odd = false
...(9)> else 
...(9)>   is_odd = true
...(9)> end
warning: variable "is_odd" is unused (if the variable is not meant to be used, prefix it with an underscore)
  iex:12

warning: variable "is_odd" is unused (if the variable is not meant to be used, prefix it with an underscore)
  iex:10

true
iex(10)> is_odd
nil

So you need to re-assign it back like below:

iex(13)> a = 5
5
iex(14)> 
nil
iex(15)> is_odd = if rem(a, 2) == 0 do
...(15)>   false
...(15)> else 
...(15)>   true
...(15)> end
true
iex(16)> is_odd
true
iex(17)> 
NobbZ

NobbZ

There was a warning about the “imperative assignment” from 1.2 or 1.3 on, and it actually worked until 1.8 or 1.9…

Where Next?

Popular in Questions Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
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
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
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 records...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement