[erlang-questions] dialyzer output help

Jesper Louis Andersen <>
Mon Aug 10 16:23:12 CEST 2015


On Mon, Aug 10, 2015 at 2:00 PM, Roelof Wobben <> wrote:

> -spec(area() -> 'ok' | 'error').


(Sean is beating me to it by a few minutes, but I have some things in here
that pushes different perspectives on his answer, so I'll post it anyway)

This is not the precise way to specify a function. I think it may work
since the parens around the specification are ignored, but I wouldn't rely
on it. See

http://erlang.org/doc/reference_manual/typespec.html

and the section "Function specifications". The notation for the above would
be

-spec area() -> 'ok' | 'error'.

Anyway, on to the error. The problem is we have a spec for prompt_user/1
that looks like the following:

-spec prompt_user(string()) -> string().

But then you define:

number_from_user(Name) ->
    Prompt = ["Enter ", Name, " > "],
    to_positive_number(prompt_user(Prompt)).

Note how the type we call with is a list(string()). This clearly breaks the
contract in the code, since you said this can't occur in the -spec. At this
point, it is often clever to ask the `typer` tool what it believes the type
is, or you can change the code: either by altering the Prompt value in
number_from_user/1 (lists:append/1 might come in handy here), or by
switching the spec:

-spec prompt_user(Input) -> string()
    when Input :: string() | [string()].

However, you may need to be more precise given that Unicode is in the game
here.


-- 
J.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150810/aa76d420/attachment.html>


More information about the erlang-questions mailing list