Search code examples
erlangpidsendmessage

message passing by variable and "direct" in erlang


I read in the book "A Concurrent Approach to Software Development" by O'Reilly
and there is in page 93, a few examples of "Message Passing".

1> Pid = self().
<0.30.0>
2> Pid ! hello.
hello
3> <0.30.0> ! hello.
* 1: syntax error before: '<'

The problem is that I don't understand why there is a syntax error, since when I type 1> Pid. I get the result <0.30.0>, so what's make the difference between line 2, and line 3 (expect from the fact that it's a variable)?


Solution

  • There is no syntax for literal process ids in Erlang. The reason for this is that it almost never makes sense: the process you want to reach most likely has a different pid every time your program runs.

    Usually you'll spawn a process, save the return value of spawn in a variable, and then use that variable to interact with the process.

    If you really want to get a usable pid from its string representation, you can use the function list_to_pid, or pid to get a pid from the printed components:

    list_to_pid("<0.30.0>")
    pid(0,30,0)