Hello everyone,
I am implementing a stack with Genserver, but I keep getting an error saying there is no process alive. Please go through the code below.
defmodule Stack.Server do
use GenServer
def start(_elements) do
IO.puts "Hello"
GenServer.start_link(__MODULE__, [4,5,"cool"])
end
def init(init_num) do
{:ok, init_num}
end
def handle_call( _from,:pop,[h|t]) do
IO.inspect [h|t]
IO.inspect [t]
IO.puts "handling call"
{:reply, {:ok,h}, t}
end
def handle_cast({:push,value},state) do
IO.inspect value
IO.puts "handling cast"
{:reply, {:ok,value}, [value|state]}
end
def pop(pid) do
IO.puts "Inside popping stack"
IO.inspect pid
GenServer.call(:pops, pid)
end
def push(stack, item) do
IO.puts "Inside pushing stack"
GenServer.cast(stack,{:push,item})
end
end
{:ok, pid} = Stack.Server.start [1,2,3]
Stack.Server.pop(pid)
Stack.Server.push(pid, 10)
Stack.Server.pop(pid)
And the error message says:
Compiling 2 files (.ex)
Hello
Inside popping stack
#PID<0.121.0>
== Compilation error in file lib/stack/server.ex ==
** (exit) exited in: GenServer.call(:pops, #PID<0.121.0>, 5000)
** (EXIT) no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started
(elixir) lib/gen_server.ex:999: GenServer.call/3
Thanks a lot in advance
PS: I am a beginner.