[erlang-questions] dialyzer: user-defined types just synonyms?

Dmitry Belyaev rumata-estor@REDACTED
Thu Apr 22 15:18:26 CEST 2010


And one more question. I have a code:
-module(test).

-export([start/0]).

-spec start() -> ok.
start() ->
    %%check_atom(bad),
    case check_atom(good) of
        ok -> ok;
        error -> error
    end.

-spec check_atom(good | nice) -> ok | error.
check_atom(Atom) ->
    ok.

Dialyzer shows:
 test.erl:10: The pattern 'error' can never match the type 'ok'
I'd like it to pay more attention to -spec than to function's body so I 
wouldn't get this message. Is it possible?

I understand that I can filter these messages with grep but this way I 
may throw away something important.

Dmitry Belyaev wrote:
>
> I want to define my type as a synonym for built-in type. But I want 
> dialyzer to warn me if a function is called with wrong type.
> I have following code and dialyzer says everything is nice. But I want 
> it to warn about "StrFromStr = my_to_str(Str)" line.
> Can I make it? How?
>
> I remember Haskell's "newtype" would be of desired behaviour.
>
> -module(test).
>
> -export([start/1]).
>
> -type my_str() :: string().
>
> -spec start(string()) -> tuple().
> start(Str0) ->
>    MyStr = str_to_my(Str0),
>    Str = my_to_str(MyStr),
>       MyFromMy = str_to_my(MyStr),
>    StrFromStr = my_to_str(Str),
>       {Str, MyFromMy, StrFromStr}.
>
> -spec str_to_my(string()) -> my_str().
> str_to_my(Str) ->
>    Str.
>
> -spec my_to_str(MyStr) -> string() when is_subtype(MyStr, my_str()).
> my_to_str(MyStr) ->
>    MyStr.
>
>
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
>
>



More information about the erlang-questions mailing list