Is there a way to translate PID in a string representation ?

3,099 views
Skip to first unread message

Emmanuel Buu

unread,
Jan 4, 2016, 8:23:24 PM1/4/16
to elixir-lang-talk
I am writting a SIP (Simple Initiation Protocol) stack in Elixir as an extended tutorial to learn Elixir.
It takes advantages of processes (each SIP transaction is a process). Now I'd like to use PID
as transaction ID but I can't find a way to translate PID to string:

iex(1)> toto = "#{self()}"
** (Protocol.UndefinedError) protocol String.Chars not implemented for #PID<0.57.0>
    (elixir) lib/string/chars.ex:3: String.Chars.impl_for!/1
    (elixir) lib/string/chars.ex:17: String.Chars.to_string/1

Peter Hamilton

unread,
Jan 4, 2016, 8:38:35 PM1/4/16
to elixir-lang-talk
I'd recommend against it. PIDs aren't guaranteed to be unique as they get recycled.

Anyway, to answer the question :erlang.pid_to_list/1 and Kernel.inspect/1 will both do what you want.

:erlang.pid_to_list/1 comes with this disclaimer:

Warning


This BIF is intended for debugging and is not to be used in application programs.


--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/ada49ba0-9891-4877-b30a-36916bec5e00%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alexei Sholik

unread,
Jan 5, 2016, 4:16:18 AM1/5/16
to elixir-l...@googlegroups.com
System.unique_integer might be a better candidate for transaction IDs.

Louis Pilfold

unread,
Jan 5, 2016, 6:37:40 AM1/5/16
to elixir-lang-talk
Hiya

unique_integer/0 seems to be in the :erlang module

Cheers,
Louis
> https://groups.google.com/d/msgid/elixir-lang-talk/CAAPY6ePKhTVq-Gsn8R5US0BZnS9LhZbjXwPFVgwoEmWkSsDpFQ%40mail.gmail.com.

José Valim

unread,
Jan 5, 2016, 6:40:46 AM1/5/16
to elixir-l...@googlegroups.com
It is part of System in Elixir v1.2.



José Valim
Skype: jv.ptec
Founder and Director of R&D

Emmanuel Buu

unread,
Jan 5, 2016, 11:36:38 AM1/5/16
to elixir-lang-talk, lo...@lpil.uk


Le mardi 5 janvier 2016 12:37:40 UTC+1, Louis Pilfold a écrit :
Hiya

unique_integer/0 seems to be in the :erlang module

Cheers,
Louis

Ok, then I will go for this ! 

 
Reply all
Reply to author
Forward
0 new messages