jacknorman
Hello guys,
I have a fairly simple question and cant seem to understand it.
for example we have the following code:
x = 1
x = 2
IO.puts x
since elixir is immutable, why does this print 2?
now i know that elixir doesnt make any change directly to x and in fact x = 2 is does on a copy of the variable x = 1. Then how can we get the original value for x, which is 1?
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Hello,
I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter).
The diffic...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
I think I’ve found a small improvement I could contribute to <%= web_namespace %>.CoreComponents (installer/templates/phx_web/compo...
New
Other Trending Topics
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
ICal is a library for interacting with iCalendar data. It parses iCalendars into typed Elixir structs via ICal.from_ics, and can prepare ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #phoenix_html
- #elixirconf-us
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
jeroenbourgois
Immutability does not mean that you cannot rebind values. It means you cannot reassign as in other languages.
For example:
What you did in your example is just bound the value of 2 to the variable x, thus the original value of 1 is lost.
LostKobrakai
Immutability is independent from assignments – the act of creating the relationship between variable and the value it represents. Immutability only means your values (in memory) will never be modified. It doesn not mean that the variable
xwill always represent the same value. Once you changed whatxpoints to there’s no way to get the value it pointed to before (even when it might still exist in memory).jacknorman
the example you provided means that value of x is being changed scope wise. For example:
so like in Rust, it gives an error and also gives you the option to declare a variable mutable or immutable, should we be getting an error when we something like the code i posted?
LostKobrakai
You’ll get an error if you bind a variable, but don’t use it within the scope it’s assigned in, but not if you use it as well.
jeroenbourgois
@jacknorman consider the response by @LostKobrakai, it is much more correct.
Another example which you might run in to when you are new to Elixir or immutabillity in general, is that you actually forget to reassign, e.g. when using lists and maps.
jacknorman
alright, so that means that values can be overwritten in elixir for variables but memory wise the values stay the same? can you please explain with an example
jacknorman
this I understand. The same concept is used when working with for loops in elixir, or when using sockets in live views and you have to do something like this for example
jeroenbourgois
Indeed. As for the example, I am no expert in Elixir either… But what I understand is that values get assigned to variables and get scoped into the block they are defined in.
So
@LostKobrakai does that make sense?
If you wanted the value from inside the
ifblock, you’d need to assign is:x will be 3
jacknorman
its a good answer but sadly still doesnt answer my question.
@LostKobrakai and @jeroenbourgois I think immutability means that whatever variable we are creating can be changed but when we look at it in the view of a process then their states cannot be changed once they are running. I saw this video and in this he explains that actually a copy is being made which is used but the original data stays the same.
hauleth
In short, both
xes in your example are different variables.It is more like:
(It will look almost exactly like that in Erlang code)
The whole pattern is called rebinding and Rust offers something similar (if that make it clearer):
Just in case of Elixir you do not need the
letprefix.To show that more explicitly in Elixir you can do: