Why GenServer does use :gen.start function during start?

You can see it here

I am curious why not to use :gen_server.start?

As the OTP documentation said, :gen:

This module implements the really generic stuff of the generic standard behaviours (e.g. gen_server, gen_fsm).

do_start is trying to abstract the logic which is shared by start_link/3 and start/3.

  @spec start_link(module, any, options) :: on_start
  def start_link(module, init_arg, options \\ []) when is_atom(module) and is_list(options) do
    do_start(:link, module, init_arg, options)   # <- notice the :link 
  end

  @spec start(module, any, options) :: on_start
  def start(module, init_arg, options \\ []) when is_atom(module) and is_list(options) do
    do_start(:nolink, module, init_arg, options) # <- notice the :nolink
  end

  defp do_start(link, module, init_arg, options) do
     # ...
  end

If using :gen_server.start for GenServer.start, you have to use :gen_server.start_link for GenServer.start_link.

If using :gen.start, we can control :link / :nolink conditionally, which leads to better organized code.

1 Like

Thank you!

1 Like