Note that there are some explanatory texts on larger screens.

plurals
  1. POUnderstanding the return value of spawn
    text
    copied!<p>I'm getting started with Erlang, and could use a little help understanding the different results when applying the PID returned from <code>spawn/3</code> to the <code>process_info/1</code> method.</p> <p>Given this simple code where the <code>a/0</code> function is exported, which simply invokes <code>b/0</code>, which waits for a message:</p> <pre><code>-module(tester). -export([a/0]). a() -&gt; b(). b() -&gt; receive {Pid, test} -&gt; Pid ! alrighty_then end. </code></pre> <p>...please help me understand the reason for the different output from the shell:</p> <hr> <p><strong>Example 1:</strong></p> <p>Here, <code>current_function</code> of <code>Pid</code> is shown as being <code>tester:b/0</code>:</p> <pre><code>Pid = spawn(tester, a, []). process_info( Pid ). &gt; [{current_function,{tester,b,0}}, {initial_call,{tester,a,0}}, ... </code></pre> <hr> <p><strong>Example 2:</strong></p> <p>Here, <code>current_function</code> of <code>process_info/1</code> is shown as being <code>tester:a/0</code>:</p> <pre><code>process_info( spawn(tester, a, []) ). &gt; [{current_function,{tester,a,0}}, {initial_call,{tester,a,0}}, ... </code></pre> <hr> <p><strong>Example 3:</strong></p> <p>Here, <code>current_function</code> of <code>process_info/1</code> is shown as being <code>tester:a/0</code>, but the <code>current_function</code> of <code>Pid</code> is <code>tester:b/0</code>:</p> <pre><code>process_info( Pid = spawn(tester, a, []) ). &gt; [{current_function,{tester,a,0}}, {initial_call,{tester,a,0}}, ... process_info( Pid ). &gt; [{current_function,{tester,b,0}}, {initial_call,{tester,a,0}}, ... </code></pre> <hr> <p>I assume there's some asynchronous code happening in the background when <code>spawn/3</code> is invoked, but how does variable assignment and argument passing work (especially in the last example) such that <code>Pid</code> gets one value, and <code>process_info/1</code> gets another?</p> <p>Is there something special in Erlang that binds variable assignment in such cases, but no such binding is offered to argument passing?</p> <hr> <p><strong>EDIT:</strong></p> <p>If I use a function like this:</p> <pre><code>TestFunc = fun( P ) -&gt; P ! {self(), test}, flush() end. TestFunc( spawn(tester,a,[]) ). </code></pre> <p>...the message is returned properly from <code>tester:b/0</code>:</p> <pre><code>Shell got alrighty_then ok </code></pre> <p>But if I use a function like this:</p> <pre><code>TestFunc2 = fun( P ) -&gt; process_info( P ) end. TestFunc2( spawn(tester,a,[]) ). </code></pre> <p>...the <code>process_info/1</code> still shows <code>tester:a/0</code>:</p> <pre><code>[{current_function,{tester,a,0}}, {initial_call,{tester,a,0}}, ... </code></pre> <p>Not sure what to make of all this. Perhaps I just need to accept it as being above my pay grade!</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