NoDisplayName
NoDisplayName

Reputation: 15736

Function overlapping specs

I have a simple function like this:

def extract_text({_, _, [text]}) when is_binary(text), do: text
def extract_text(_), do: nil

and the spec I added for it was:

@spec extract_text(any) :: nil
@spec extract_text({any, any, [text]}) :: text when text: String.t

but when I run dializer, I get the following error:

lib/foo/bar.ex:1: Overloaded contract for 'Elixir.Foo.Bar':extract_text/1 has overlapping domains; such contracts are currently unsupported and are simply ignored

I think I understand the reason for it but I can't really come up with a solution. What would be the right spec for this function?

Upvotes: 2

Views: 1001

Answers (1)

Paweł Dawczak
Paweł Dawczak

Reputation: 9639

You should be aware of that, even if you define multiple functions of the same arity (accept the same number of arguments), from outside world this is considered only one function. That means, you need to define function signature, and only this one should have type spec defined.

Try the following:

@spec extract_text(any) :: String.t | nil
def extract_text(arg)

def extract_text({_, _, [text]}) when is_binary(text), do: text
def extract_text(_), do: nil

Upvotes: 5

Related Questions