How to return extra information when starting a GenServer

Looking through the documentation on supervisors it looks like the support process that return more than their pid when starting. The second value of the on_start type is {:ok, child, term()}. However I cannot see how to return that third term when using a GenServer.

I wan’t something like the following

defmodule MyServer do
  use GenServer

  def init() do
    {:ok, :state, :term}

{:ok, pid(), :term} = DynamicSupervisor.start_child(DynamicSupervisor, MyServer)

I have made an implementation of MyServer that works by using :proc_lib but not with GenServer. It seams strange that a feature available in supervisors can not be used with Gen* modules.

For reference what I want to return as the last term is a via tuple so I can experiment with persistent/virtual actor implementations

I wanted to do this too, but it seems discouraging, since the erlang documentation for supervisor says:

The start function must create and link to the child process, and must return {ok,Child} or {ok,Child,Info}, where Child is the pid of the child process and Info any term that is ignored by the supervisor.

It might be ignored by the supervisor. However that doesn’t matter because it is still returned by the supervisor to the calling process

It’s supported by the supervisor as input, but the standard behaviours (like GenServer) never return such a value from start_link.

1 Like

Ok. so that is what I concluded. would there be resistance to making it so GenServer could return an info term? I certainly would find it very useful

That’s probably a question to the OTP team, but I expect they would need concrete use-cases.

You could always start the process manually with proc_lib and call gen_server:enter_loop at the end to make it into a GenServer.

1 Like

So it’s blocked at the erlang level and not something that the Elixir wrapper has decided not to implement?

Judging by the documentation OTP proper settled on {ok,Pid} while {ok,Pid,Info} was an accommodation for special processes which needed to use proc_lib:start(_link) which returns an arbitrary term upon success.

It would most probably require dropping the Erlang implementation and reimplementing at least the initialization part before gen_server:enter_loop in Elixir, so I would say it’s unlikely.

That makes sense. I guess I will have to stick with proc_lib.