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

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
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
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
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
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
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement