Sanjibukai
Hello everybody,
I just noticed something that I admit I should have been notice way more earlier TBH..
Variables are scoped in ifs, conds, cases (and I bet it’s applicable to any block).
An example with a simplified snippet:
def doing_something do
a = 1
...
if some_true_condition do
a = 2
...
end
IO.inspect(a) # > Still 1 even if some_true_condition is true
end
Now I’m even surprised of how I didn’t got any bugs so far related to this behavior I wasn’t aware.
Maybe I’ve embraced FP well more than I think (so i’m glad).
But until now I didn’t come across (or maybe I didn’t notice) any resource explaining this behavior.
And it’s a really important behavior!
I searched on hexdocs.pm and looked on almost all the guides pages on elixir-lang and this is the only result I found, a changelog for version 1.3 where the following is stated:
Elixir will now warn if constructs like
if,caseand friends assign to a variable that is accessed in an outer scope.
And contrary to that there isn’t any warning anymore (on v1.10 btw).
I thought replacing the if construct by assigning from the whole block like so:
a =
if some_true_condition do
...
2
end
But in this case we are still changing the value with nil if some_true_condition is actually false instead of doing nothing.
If you happen to have to do something equivalent (changing a value in a block) how do you handle it?
Also if you have any more information about this behavior, I’m interested to learn more.
Thank you!
Trending in Discussions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










First Post!- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
kokolegorille
It is more about scoping than FP…
I would do it like this if needed
Most Liked
lud
Remember that you are not changing the value of
a. What you are really doing is creating a new variable with the same name (and deleting the old variable).benwilson512
What you’re proving is precisely that rebinding isn’t mutability. The function closes over the value of
x. That value isn’t changed when you rebind x. If it changed in the function thenxis in fact not immutable. Javascript does not have immutability, so it behaves the way you observe. Closures in immutable languages behave the way you observed in Elixir.al2o3cr
FWIW, I find myself using this with
Ecto.Multiwhen operations should only be done sometimes:Last Post!
LostKobrakai