hst337

hst337

Magic functions you probably didn't know about

Magic BIFs and NIFs in OTP and Elixir

For the last few weeks I was working on purity and effects analyzer of Elixir and Erlang projects, and I came across a bunch of interesting functions which were built-in but undocumented or hidden.

Please, do not use them in production systems because they’re a subject to change
But don’t hesitate to play with them to check how the erlang runtime works, or make some dark magic there

  • :erts_internal.cmp_term compares two terms in oldschool -1, 0, 1 fashion
  • :erts_internal.term_type, function which returns a top-level type of the structure. Interesting thing is that for [] it returns nil and for nil it returns :atom. This function is really useful for debugging of binaries
  • :erts_internal.map_to_tuple_keys to show keys tuple for flatmaps (maps with <= 32 keys)
  • :erts_internal.map_hashmap_children to show the Tree representation of the hashmap (map with > 32 keys)
  • :erts_internal.ets_super_user to make the current process become a ETS process with maximum privileges. Process will have read/write access to any ets table (even protected and private ones)
  • :binary.referenced_byte_size (documented) to check a size of a referenced binary
  • :prim_eval.receve which works like a normal receive, but allows to decide whether to pick or not to pick the message from the queue to the closure rather then a pattern. It allows to peek in the head of the queue, without taking the value out of queue
  try do: :prim_eval.receive(fn x -> throw(x) end, 0), catch: (x -> x)
  • :erts_debug.same to check if the values are represented by the exactly same structure in memory (this means that pointers for heap values are equal)
  • :erts_debug.size and :erts_debug.flat_size to check the size with term reusage and flat size like if every term was referenced only once.

If you know any other magic functions, please post them in this thread :hugs:

Most Liked

sabiwara

sabiwara

Elixir Core Team

This one is documented but :erlang.phash2/2 can be quite useful to hash any arbitrary term as an integer within a range.

iex> :erlang.phash2({:works, %{with: ["just", "anything"]}}, 1000)
915
# default range of 2^27
iex> :erlang.phash2({:works, %{with: ["just", "anything"]}})
28206459
evadne

evadne

I have a practical one:

:erlang.display_string '\a'

:joy:

Where Next?

Popular in Discussions Top

Jayshua
I recently came across the javascript library htmx. It reminded me a lot of liveview so I thought the community here might be interested....
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
mbenatti
Following https://github.com/tbrand/which_is_the_fastest |&gt; https://raw.githubusercontent.com/tbrand/which_is_the_fastest/master/imgs...
New
CharlesO
Erlang :list.nth simple, but 1 - based nth(1, [H|_]) -&gt; H; nth(N, [_|T]) when N &gt; 1 -&gt; nth(N - 1, T). Elixir Enum.at … coo...
New
marciol
Please, let me know if this kind of discussion already took place in another topic . Hi all, how do you consider if is better to build ...
New
PragTob
Hey everyone, this has been on my mind for some time and I’d love your input on it! TLDR: I feel like maps are superioer for storing and...
New
rms.mrcs
A couple of days ago I was discussing with a friend about different approaches to write microservices. He said that if he was going to w...
New
RudManusachi
What configs will make sense to put to runtime.exs? – A bit of how I configure apps: I have generic configs in config/config.exs, dev...
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
griffinbyatt
Sobelow Sobelow is a security-focused static analysis tool for the Phoenix framework. For security researchers, it is a useful tool for g...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
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
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 127089 1222
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement