Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>When you do a <code>spawn</code> you create a new completely <strong><em>new</em></strong> process with its own environment and thread of execution. This means that you are no longer executing "inside" the module where the <code>spawn</code> is called, so you must make an "outside" call into the module. the only functions in a module which can be called from the "outside" are exported functions, hence the spawned function must be exported.</p> <p>It might seem a little strange seeing you are spawning a function in the same module but this is why.</p> <p>I think it is important to remember that a module is just code and does not contain any deeper meaning than that, for example like a class in an OO language. So even if you have functions from the same module being executed in different processes, a <strong><em>very</em></strong> common occurrence, then there is no implicit connection between them. You still have to send messages between processes even if it is from/to functions in the same module.</p> <p><strong>EDIT:</strong></p> <p>About the last part of your question with the quote about putting export <code>init/1</code> in a separate export declaration. There is no need to do this and it has no semantic significance, you can use as many or as few export declarations as you wish. So you could put all the functions in one export declaration or have a separate one for each function; it makes no difference.</p> <p>The reason to split them is purely visual and for documentation purposes. You typically group functions which go together into separate export declarations to make it easier to see that they are a group. You also typically put "internal" exported functions, functions which aren't meant for the user to directly call, in a separate export declaration. In this case <code>init/1</code> has to be exported for the spawn but is not meant to be called directly outside the spawn.</p> <p>By having the user call the <code>start/0</code> function to start the server and not have them explicitly spawn the <code>init/1</code> function allows you to change the internals as you wish later on. The user only sees the <code>start/0</code> function. Which is what the first quote is trying to say.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload