-
-
Save rodrigues/d98570de7808a6fe26a784f04cf672b9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule DialyzerExamples do | |
# ** Dialyzer will alert if type is a superset of actual type, | |
# if you have `underspecs` flag on: | |
# | |
# lib/dialyzer_examples.ex:16:contract_supertype | |
# Type specification is a supertype of the success typing. | |
# | |
# Function: | |
# DialyzerExamples.foo/1 | |
# | |
# Type specification: | |
# @spec foo(any()) :: any() | |
# | |
# Success typing: | |
# @spec foo(number()) :: number() | |
@spec foo(any()) :: any() | |
def foo(i), do: i + 1 | |
def bar(i) do | |
case foo(i) do | |
n when is_number(n) -> | |
"yes" | |
_ -> | |
# ** Dialyzer will alert you this path can never match, | |
# no matter what your typespecs actually say: | |
# | |
# lib/dialyzer_examples.ex:24:pattern_match_cov | |
# The pattern | |
# :variable_ | |
# | |
# can never match, because previous clauses completely cover the type | |
# number(). | |
"never" | |
end | |
end | |
# ** Dialyzer will spot if you're missing extra types in your typespecs, | |
# if you have the flag `specdiffs` on: | |
# | |
# lib/dialyzer_examples.ex:38:missing_range | |
# The type specification is missing types returned by function. | |
# | |
# Function: | |
# DialyzerExamples.errorable_fun/1 | |
# | |
# Type specification return types: | |
# ({:ok, _}) | |
# | |
# Extra types in success typing: | |
# ({:error, :something_else}) | |
@spec errorable_fun(term()) :: {:ok, term()} | |
def errorable_fun(something) do | |
if something do | |
{:ok, something} | |
else | |
{:error, :something_else} | |
end | |
end | |
def run(whatever) do | |
# ** Dialyzer will tell you if a function you call can return something | |
# other than `:ok` and you're not considering it, | |
# if you have the flag `unmatched_returns` enabled: | |
# | |
# lib/dialyzer_examples.ex:59:unmatched_return | |
# The expression produces a value of type: | |
# | |
# {:error, :something_else} | {:ok, _} | |
# | |
# but this value is unmatched. | |
errorable_fun(whatever) | |
:ok | |
end | |
# ** Dialyzer will tell you if you added an extra type in your typespec | |
# that can never be returned, by default: | |
# | |
# lib/dialyzer_examples.ex:76:extra_range | |
# The type specification has too many types for the function. | |
# | |
# Function: | |
# DialyzerExamples.baz/0 | |
# | |
# Extra type: | |
# {:error, :nope} | |
# | |
# Success typing: | |
# :ok | |
@spec baz() :: :ok | {:error, :nope} | |
def baz do | |
:ok | |
end | |
defmodule Be do | |
@callback foo() :: :ok | |
end | |
defmodule Impl do | |
@behaviour Be | |
# ** Dialyzer will alert you if a callback | |
# is not being respected: | |
# | |
# lib/dialyzer_examples.ex:112:callback_type_mismatch | |
# Type mismatch for @callback foo/0 in DialyzerExamples.Be behaviour. | |
# | |
# Expected type: | |
# :ok | |
# | |
# Actual type: | |
# :nope | |
def foo, do: :nope | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment