Hal9000

Hal9000

Variables leaking

I’ve been thinking about the change made 2-3 years ago where it’s
no longer permitted to set variables inside a case statement (or similar).

Is it fair to summarize this by saying: Variable values can pass from an
outer context to a more inner one, but those variables cannot be bound
to new values (such that the outer context changes)?

This afaik applies to any “block” of code nested inside a larger context –
the “guts” of an if, case, cond, code passed to a macro…

Am I looking at this correctly or not?

Hal

First Post!

hauleth

hauleth

In short ways, binding is valid only within it’s block.

x = 1

if true do
  # we start new block there
  x = 2
  # there `x` has new binding
end # block ended so all it's bindings as well

# there `x` has meaning of it's previous binding

Last Post!

Hal9000

Hal9000

Yes, thank you…

Of course, the value of x is still known inside the block until it is reassigned…

x = 1
IO.puts x

if true do 
  IO.puts x
  x = 2     
  IO.puts x
end 

IO.puts x

# Prints: 1, 1, 2, 1

Hal

Where Next?

Popular in Discussions Top

boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
AstonJ
If so I (and hopefully others!) might have some tips for you :slight_smile: But first, please say which area you’re finding most challen...
New
PragTob
Hello everyone, I know we had quite some threads (read through lots of them) about background job processing but it remains a hotly deba...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
eteeselink
Hi all, In the last days, two things happened: A blog post titled “They might never tell you it’s broken” made the rounds. It’s about ...
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
crispinb
On reading dhh’s latest The One Person Framework it strikes me that Phoenix with LiveView is already pretty much this. However, never hav...
New

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
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
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

We're in Beta

About us Mission Statement