def add(pid, x, y) do send(pid, {:add, self(), x, y}) receive do {:result, x} -> x end end def loop(state) do receive do {:add, from, x, y} -> send(from, {:result, x + y}) end loop(state) end end
def add(pid, x, y) do send(pid, {:add, self(), x, y}) receive do {:result, x} -> x end end def loop(state) do receive do {:add, from, x, y} -> send(from, {:result, x + y}) end loop(state) end end
with :sys.debug_options/1 • respond to start_link with :proc_lib.init_ack/2 • use :sys.handle_system_msg/6 for {:system, from, request} • implement system_continue/3 and system_terminate/4 callbacks
... @callback some_callback(term) :: {reply :: term, state :: term} def start_link(module, args, opts) do GenServer.start_link(__MODULE__, {module, args, opts}, opts) end def init({mod, args, opts}) do case mod.init(args) do {:ok, int} -> %{mod: mod, int: int} other -> other end end
do {reply, int} = mod.some_callback(arg, int) {:reply, reply, %{state | internal: int}} end def format_status(:normal, [pdict, %{mod: mod, int: int}]) do [{:data, [{'State', int}]}] end def format_status(:terminate, [pdict, %{int: int}]) do int end