[erlang-questions] encapsulating types and dialyzer

Tobias Lindahl <>
Mon Sep 15 09:26:07 CEST 2008



Anthony Shipman wrote:
> On Fri, 12 Sep 2008 05:13:45 pm Tobias Lindahl wrote:
>>> PS. I should point out that the language of types and -spec declarations
>>>      is currently an EEP.  Officially, it is not part of Erlang yet.
> 
> Why do some of the types in the Erlang source have extra parentheses around 
> them?
> 	-type(digraph() :: any()).
> Is there any use for this or is it just an ornament?

This used to be the only way to declare them. The declaration without 
parenthesis is brand new.

> What could be useful to include is a "new type" feature like in Ada or 
> Haskell. E.g.
> 
> -type new length() :: float().
> -type new area() :: float().
> 
> Then although they are both implemented as floats you can't pass one as 
> another without an error or warning.

In Erlang the types are nothing but aliases as there are no type 
constructors apart from the primitive ones. In runtime, there are no 
length() and area(), but only float().

-type length() :: float().
-type area() :: float().

works just fine. You might want to have a look at 
http://user.it.uu.se/~tobiasl/publications/succ_types.pdf for more 
description of what is happening under the hood.

We are looking at having opaque types that are not allowed to be 
inspected which would be more close to what you seem to expect from the 
type declarations, but the nature of Erlang (dynamic typing, hot code 
loading, etc) makes it hard to do proper type checking without changing 
the language quite extensively. Instead, we have chosen to loosen up the 
type inference/checking quite a bit to better suit Erlang's programming 
idiom.

Tobias

> 
> The change to the type checking algorithm might be quite small. When comparing 
> two types, if they are both new then the names must match as well as the 
> structure, otherwise only do structural matching.



More information about the erlang-questions mailing list