kokolegorille

kokolegorille

Different closure between Erlang and Elixir

Hello everyone,

Here is some erlang code I wanted to translate to Elixir… here is a simple example in the console.

$ erl
Erlang/OTP 22 [erts-10.4.4] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe]

Eshell V10.4.4  (abort with ^G)
1> Pid=self().                            
<0.82.0>
2> Ref = monitor(process, Pid).              
#Ref<0.2147538226.2721841158.105393>
3> receive                                
3> {'DOWN', Ref, process, Pid, Why} -> Why
3> end.

and here is the Elixir version I came up with…

$ iex
Erlang/OTP 22 [erts-10.4.4] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe]

Interactive Elixir (1.9.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> pid = self()
#PID<0.108.0>
iex(2)> ref = Process.monitor(pid)
#Reference<0.417878796.1379926020.235519>
iex(3)> receive do
...(3)> {:DOWN, ref, :process, pid, why} -> why            
...(3)> end
warning: variable "pid" is unused (if the variable is not meant to be used, prefix it with an underscore)
  iex:4

warning: variable "ref" is unused (if the variable is not meant to be used, prefix it with an underscore)
  iex:4

Code is quite similar, but in the Elixir version, ref and pid are not seen by the receive block, while the Erlang version will match on a ‘DOWN’ message where Ref and Pid are set.

Why is that different?

Thanks for taking time

Marked As Solved

hauleth

hauleth

Elixir allows to rebind the variables, so this is perfectly possible to do:

a = 1
a = 2

Which will then be translated to Erlang as a something like:

A@1 = 1
A@2 = 2

To have the same behaviour as in Erlang, you need to use pin operator (^):

a = 1
^a = 2 # will fail with no match

So in your case you need to write it as:

receive do
  {:DOWN, ^ref, :process, ^pid, why} -> why
end

To “pin down” the variables to their current binding instead of creating new ones.

Also Liked

al2o3cr

al2o3cr

Erlang doesn’t distinguish between binding a value and pattern-matching on an already-bound value (part of its Prolog heritage IIRC). Elixir disambiguates between these two cases with the ^ operator, referred to as the “pin operator”:

https://hexdocs.pm/elixir/Kernel.SpecialForms.html#^/1

So this (from your example) binds new values to ref and pid:

receive do
  {:DOWN, ref, :process, pid, why} -> why
end

while this pins them to their already-bound values:

receive do
  {:DOWN, ^ref, :process, ^pid, why} -> why
end
kokolegorille

kokolegorille

Thanks, I forgot the pin :slight_smile:

kokolegorille

kokolegorille

Thanks, I need a :coffee: :slight_smile:

This is the full function

  def on_exit(pid, fun) do
    spawn(fn ->
      ref = Process.monitor(pid)
      receive do
        {:DOWN, ^ref, :process, ^pid, reason} -> fun.(reason)
      end
    end)
  end

Where Next?

Popular in Questions Top

freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

Other popular topics Top

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...
3268 119930 1237
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement