garecinos4

garecinos4

Functional conditions

I noticed in the 1.7.4 version the condition IF to handle its own scope, and it does not allow to persist the variable inside and neither do modifications of variables, I wonder why this change

Example:
Input:
a = 23
if true, do: a = 4
Output:
a = 23

I wonder if there is any documentation about this

Marked As Solved

peerreynders

peerreynders

a = 23
if(true, do: a = 4)
warning: variable "a" is unused

Note variables defined inside case, cond, fn, if and similar do not leak. If you want to conditionally override an existing variable "a", you will have to explicitly return the variable. For example:

    if some_condition? do
      atom = :one
    else
      atom = :two
    end

should be written as

    atom =
      if some_condition? do
        :one
      else
        :two
      end

Unused variable found at:

elixir/CHANGELOG.md at v1.7 · elixir-lang/elixir · GitHub

[Kernel] Raise on unsafe variables in order to allow us to better track unused variable

Release v1.7.0 · elixir-lang/elixir · GitHub

[Kernel] Raise on unsafe variables in order to allow us to better track unused variables (also known as imperative assignment / variable leakage)

Also Liked

Qqwy

Qqwy

TypeCheck Core Team

As a side note: It is always possible to refactor such ‘assignments from within if-statements’ code to more functional code by assigning the outcome of the if-statement to something:

Last Post!

Qqwy

Qqwy

TypeCheck Core Team

As a side note: It is always possible to refactor such ‘assignments from within if-statements’ code to more functional code by assigning the outcome of the if-statement to something:

Where Next?

Popular in Questions Top

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
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
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
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
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

Other popular topics 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
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 49084 226
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
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
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
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42533 114
New

We're in Beta

About us Mission Statement