Background
I am reading “ Functional Web Development with Elixr, OTP, and Phoenix ” and there is a code sample that is setting of an error with dialyzer, an error which I believe is incorrect (should not be an error). This is rather confusing, as I have read that if dyalizer tells you something is wrong, then something is wrong for sure.
@impl GenServer
def handle_call({:position_island, player, key, row, col}, _from, state) do
board = player_board(state, player)
reply_error = create_error_reply(state)
with {:ok, rules} <- Rules.check(state.rules, {:position_islands, player}),
{:ok, coord} <- Coordinate.new(row, col),
{:ok, island} <- Island.new(key, coord),
%{} = board <- Board.position_island(board, key, island)
do
state
|> update_board(player, board)
|> update_rules(rules)
|> reply_success(:ok)
else
:error ->
reply_error.(:error)
{:error, :invalid_coordinate} ->
reply_error.({:error, :invalid_coordinate})
{:error, :invalid_island_type} ->
reply_error.({:error, :invalid_island_type})
{:error, :overlapping_island} ->
reply_error.({:error, :overlappign_island})
end
end
Error
It points to the line:
{:ok, island} <- Island.new(key, coord),
and tells me that I am not watching out for the errors {'error','invalid_coordinate' | 'invalid_island_type'}
:
lib/islands_engine/game.ex:88: The pattern {‘ok’, _island@1} can never match the type {‘error’,‘invalid_coordinate’ | ‘invalid_island_type’}
Which is not true, as you can see I have an else clause that checks for this.
Why am is dialyzer screaming about this?