[erlang-questions] Typespecs export not working as intended?

Kostis Sagonas kostis@REDACTED
Tue Apr 19 11:18:51 CEST 2011

Loïc Hoguin wrote:
> Hello,
> I have ran into a weird issue while refactoring the type specs in a project.
> It seems types in a module can be used even if they are not exported
> using export_type. Neither of the compiler or dialyzer are complaining
> about it.

As others have explained the compiler does not complain when using a 
type from another module because:

    1) this would require that it looks at the code of the other module
    2) it would be inconsistent with what the compiler does with remote 
calls to functions that are not exported.

But dialyzer *does* complain when you analyze both modules at the same 
time -- though perhaps not loudly enough.

> For example, module a declares a type my_type(), module b uses it with
> a:my_type(), no export_type in module, yet it still works.
> Is that normal? The documentation clearly states that types should be
> exported.

Can you please be a bit more specific about the behavior you are 
experiencing?  A concrete example and information about the version of 
OTP you are using will help a lot to understand what you are talking 
about.  Also please send the dialyzer command you are using.  I cannot 
reproduce what you claim you are experiencing.

An example below:

%%-------------- MODULE a ----------------


-spec foo(b:b()) -> string().

foo(A) when is_atom(A) -> atom_to_list(A).
%%-------------- MODULE b -----------------


-type b() :: integer().  % NOT EXPORTED

-spec bar(b()) -> b().
bar(X) when is_integer(X) -> X + 42.

$ dialyzer a.erl b.erl
   Checking whether the PLT /home/kostis/.dialyzer_plt is up-to-date... yes
   Proceeding with analysis...
Unknown types:
  done in 0m1.16s
done (passed successfully)

The b:b() type is mentioned as unknown to the analysis.  Perhaps the 
message should explicitly say that this type is not exported, but do
you get a different behavior when you run dialyzer?


More information about the erlang-questions mailing list