jslearner

jslearner

a = 1
a = 2

How can the first assignment a = 1 be immutable after the a = 2 rebind?

Showing Posts 1 to 10

NobbZ

NobbZ

The value of 1 hasn’t changed, though it’s not accessible under the name a anymore, as there is a new thing named a with the value 2 “shadowing” the old a.

lud

lud

You can try this code:

a = 1
f = fn -> a end
a = 2
IO.puts("a: #{a}")
IO.puts("f(): #{f.()}")
a: 2
f(): 1

But as often, it has nothing to do with immutability, it is just how scoping rules work in elixir.

jslearner

jslearner OP

So the first 1 is still in memory?

lud

lud

if you run this code only:

a = 1
a = 2

The 1 would be on the stack memory segment, so it would not exist anymore. But I think the compiler will just skip that value completely.

josemrb

josemrb

How can the first assignment a = 1 be immutable after the a = 2 rebind?

First you need to know that the = operator is called the match operator, it is not called the assignment operator.

So, Elixir can make the match a = 1 true by binding the a variable to the specified value.

Hence, what happens next in Elixir is that the variable is rebound to the new value when Elixir is making the match a = 2 true.

Take into account that Elixir will only bind variables on the left hand side of the match operator.

NobbZ

NobbZ

Yes, from the beams point of view, the first a is actually named Va_1, the second one is Va_2, though from elixir you can only ever “see” the last binding from the current scope.

jslearner

jslearner OP

Any way to access previous binding?

NobbZ

NobbZ

As I said, no.

Exadra37

Exadra37

As I always say, variables are mutable in Elixir, no matter what fancy explanations are given to me :wink:

In my point of view, whats matter, its what I can see from outside the box, not what’s going inside the box.

Outside the box, the developer will not have the guarantee that a variable keeps the value it was first assigned, thus for me they are mutable.

Inside the box the Beam just points the variable to other location in memory where the new value its stored, or something similar, and normally is referred as rebind, thus its why everyone says that they are immutable.

In the end of the day what matters for me as a developer is the public interface the language presents to me, and I don’t care how its done under the hood(inside the box), I just care how it looks without the need to look inside the box.

NOTE: And yes I already have read Comparing Elixir and Erlang variables « Plataformatec Blog

mindok

mindok

You do, though, absolutely have a guarantee that nothing inside the box can change a variable that you have assigned.

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
csmr
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley: Mutonex gamedesign was finish...
New
Stef-Verniers
Hello I’ve been coding in Elixir for almost two years now because of my current job where we implemented Elixir in our application. But ...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews