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

Kostis Sagonas <>
Thu Apr 22 15:58:27 CEST 2010


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?

You cannot make it.  The notion of equality between types is structural, 
not nominative. (http://en.wikipedia.org/wiki/Structural_type_system)

Kostis

> 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:



More information about the erlang-questions mailing list