Mukul Chakravarty
Mukul Chakravarty

Reputation: 193

Spawning a process in elixir

I spawned a process in elixir using two ways :

defmodule Second do
   def called do
     raise "oops" 
   end

end

spawn(Second.called)
** (RuntimeError) oops
second.exs:3: Second.called/0

 spawn(Second,:called,[])
 #PID<0.89.0>
 iex(2)> 17:42:40.999 [error] Process #PID<0.89.0> raised an exception
 ** (RuntimeError) oops

What is the difference in the two methods ? Why is only the second one returning a process id and not the first one ? Thanks

Upvotes: 1

Views: 723

Answers (2)

chrismcg
chrismcg

Reputation: 1286

In the first one you're calling Second.called directly and that raises so the spawn never gets called. The second version will call spawn and the new process will call Second.called with no args which then raises.

Upvotes: 3

Ju Liu
Ju Liu

Reputation: 3999

In the first example, the code is blowing up before getting to the actual spawn. I imagine that what you wanted to do is spawn(&Second.called/0), which will return the same error as the second example.

Upvotes: 2

Related Questions