[erlang-questions] type specification

Kostis Sagonas kostis@REDACTED
Wed Jul 7 11:34:56 CEST 2010


Zoltan Lajos Kis wrote:
> Shouldn't I be warned that b:f breaks the opaqueness of a:my_type() with 
> the modules below?

Well, first of all there is no such should as far as dialyzer is 
concerned. The only property that dialyzer guarantees is that it will 
never produce a warning that is not a discrepancy. Dialyzer has nowhere 
mentioned that it will identify all discrepancies in your program. 
There is a fundamental reason why this is so: so that it guarantees that 
it will never produce a warning that is not a discrepancy ;-)

This property aside, the reason why currently you get no such warning is 
that there is no information flow between the module defining the opaque 
data type and the module where the data type is used. (In fact, in your 
example, the module defining the opaque data type does not really 
construct any such term.) Write in module 'a' a function that constructs 
an opaque term, i.e.

-spec new() -> my_type()
new() -> {foo, bar}.

and use this in module 'b' as follows:

use() ->
    T = a:new(),
    f(T).

and you will get a warning.  I hope it is clear what's happening.

> --------------------------------------------
> -module(a).
> -compile(export_all).
> 
> -opaque( my_type() :: {atom(), atom()} ).
> 
> -export_type([my_type/0]).
> 
> -spec( f(my_type()) -> atom() ).
> f({A,_B}) -> A.
> ---------------------------------------------
> -module(b).
> -compile(export_all).
> 
> -spec( f(a:my_type()) -> atom() ).
> f({A,_B}) -> A.
> ---------------------------------------------

Now, having written all the above, in the current development version of 
dialyzer we have made, long before your mail, an important enhancement 
to the analysis that it will allow you to get the warning you desire. 
Indeed, I get:

b.erl:5: Function f/1 has no local return
b.erl:5: The attempt to match a term of type a:my_type() against the 
pattern {A, _B} breaks the opaqueness of the term


Stay tuned for R14B!

Kostis


More information about the erlang-questions mailing list