vonH

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?

Marked As Solved

RobinSJ

RobinSJ

:ok is 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.MyRouter function 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 different :slight_smile:

Also Liked

OvermindDL1

OvermindDL1

Atoms are compiled into the code when loaded in the VM (not at compile-only time), if an :ok is in code then it is statically defined. In essence this happens:

  • Code is loaded into the VM
  • The VM runs passes over the code
  • One of those passes looks at atoms, looks up the atom name (up to 255 bytes of basically anything) in the global atom table, if it finds it then it replaces the atom in the source with an integer of the index into that table, if it is not found then it makes a new entry and uses that index
  • If multiple nodes connect then the first time they communicate any atom between them they creating a mapping of indexes to indexes to make for fast communication

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_string on 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

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 :ok in 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

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 :ok is :ok :slight_smile:

With 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 :slight_smile:

Last Post!

bodhilogic

bodhilogic

You answered the question I was wondering about, “Why aren’t atoms Garbage Collected”, perfectly!
Thanks

Where Next? Top

Trending in Questions Top

RSP87
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
nseaSeb
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
RemyXRenard
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
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
samoloth
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
FlyingNoodle
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 Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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
Dmk
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews