I’m still relatively new to Elixir, so I’m hoping someone can shed some light on what’s up with my mode and/or dialyzer:
defmodule Eclipse.Router.Connection do
@moduledoc """
Server for a connection
"""
use GenServer, restart: :transient
@local_opts [:binary, packet: :raw, active: false, reuseaddr: true, reuseport: true]
@remote_opts [:binary, :inet, active: false, packet: :raw]
@type state :: %{
socket: Eclipse.Router.Socket.t()
}
@impl GenServer
@spec init({Eclipse.Config.Connections.t(), :local}) :: {:ok, state()}
def init({%Eclipse.Config.Connections{} = config, :local}) do
{:ok, socket} = :gen_tcp.listen(config.local_port, @local_opts)
{:ok, client} = :gen_tcp.accept(socket)
{:ok, %{socket: Eclipse.Router.Socket.new(client)}}
end
@spec init({Eclipse.Config.Connections.t(), :remote}) :: {:ok, state()}
def init({%Eclipse.Config.Connections{} = config, :remote}) do
{:ok, socket} = :gen_tcp.connect(config.remote_hostname, config.remote_port, @remote_opts)
{:ok, Eclipse.Router.Socket.new(socket)}
end
end
The above code passes, but the second init
has the wrong success typing. {:ok, Eclipse.Router.Socket.new(socket)}
as opposed to {:ok, %{socket: Eclipse.Router.Socket.new(socket)}}
If I comment out the first init
, I get the expected errors.
Some guidance is appreciated on what I’m doing wrong.