vonH
When looking at the elixir-lang docs on tuples iex> {:ok, "hello"}, tuple = {:ok, "hello"} , I see :ok being introduced out of thin air (unless it is introduced elsewhere), but when I try this sequence
iex -S mix
c "myfuncs/funcs_router.ex"
{:df, _} = Plug.Adapters.Cowboy.http MyRouter, []
I get the error report
** (MatchError) no match of right hand side value: {:ok, #PID<0.185.0>}
so I assume that :ok is a special atom. I can see atoms like :nil, :true and :false. Is there a list of special atoms in Elixir that can’t be used elsewhere?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
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
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Marked As Solved- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
RobinSJ
:okis a atom, like all other atoms it will not be garbage collected.The reason you’re getting a match error, is because the
Plug.Adapters.Cowboy.http.MyRouterfunction will return a tuple in the format of{:ok, pid}(This is very common in Erlang/Elixir).The tuple
{:df, _}will not match the tuple{:ok, pid}because the atoms are differentAlso Liked
OvermindDL1
Atoms are compiled into the code when loaded in the VM (not at compile-only time), if an
:okis in code then it is statically defined. In essence this happens:That is why it cannot be garbage collected, it may be used by other nodes once connected, it is used inside the compiled code in the VM, etc…
There are some functions that directly access the atom table, one of those is calling
to_stringon an atom, it looks up the atom’s index in the atom table and returns the string. Another can get the atom index of a string and create it if it does not exist, and another does the same but will error if it does not exist (always use that one if you need it).So it does not matter where it is defined or used or anything, every module in every location gets the same atom index. It is only set at VM time, not compile-time.
OvermindDL1
Also think of it this way since you implied you use C and such in other posts, think of an atom as a named integer, every one is unique in the system and any of the same name will always match any other very fast. Quite literally at compile time the atom
:okin code is replaced with an integer so the comparisons are integer-fast (though the integer is packed in the tagged type properly, just like integers but a different tag).RobinSJ
Atoms are never assigned any value. You can think of them as constants, where the value of the atom is the name of it. The value of the atom
:okis:okWith regards to atoms not being garbage collected, every atom declared in the lifetime of a program will be stored in an atom table. If you create atoms dynamically (through user-input), at some point the atom table will consume too much memory and the VM will be killed. This is just something to be aware of
Last Post!
bodhilogic
You answered the question I was wondering about, “Why aren’t atoms Garbage Collected”, perfectly!
Thanks