I am trying to learn how dialyzer works. I am using an example from the documentation for ecto migrations found here: Deploying with Releases — Phoenix v1.8.8
specifically:
defmodule MyApp.Release do
@app :my_app
def migrate do
for repo <- repos() do
{:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :up, all: true))
end
end
def rollback(repo, version) do
{:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, to: version))
end
defp repos do
Application.load(@app)
Application.fetch_env!(@app, :ecto_repos)
end
end
I ran dialyzer and got this output:
lib/my_app/release.ex:17:unmatched_return
The expression produces a value of type:
:ok | {:error, _}
but this value is unmatched.
I am also using ElixirLS in VSCodium (VSCode) and it suggests these types:
defmodule MyApp.Release do
@app :my_app
# here
@spec migrate :: [any]
def migrate do
for repo <- repos() do
{:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :up, all: true))
end
end
# here
@spec rollback(atom, any) :: {:ok, any, any}
def rollback(repo, version) do
{:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, to: version))
end
# origin of warning
# no type suggested
defp repos do
Application.load(@app)
Application.fetch_env!(@app, :ecto_repos)
end
end
I have two questions.
-
Can the suggested
@spec’s be improved or are these suggestions ideal? For example, the rollback function takes a version which was typed toanybut wouldn’t it be better if I type it to an integer? -
The repos function warning can be silenced by using the
_ =return pattern on both lines. I’m wondering if there is a type I can apply to that function that would silence it?
Perhaps I should ask, how would you type these functions in order to work with dialyzer?
Thank you






















