[erlang-questions] soft/optional typing

Tobias Lindahl tobias.lindahl@REDACTED
Wed Jun 13 11:09:49 CEST 2007


jm wrote:
> This sounds promising. Not having read the papers yet; Do you know what
> the syntax will be? Can you give a simple example?

The basic type syntax is an extended version of the syntax in the paper 
on typer and it is also similar to the edoc syntax. You give your 
contracts on a per-function basis as a compiler attribute.

-spec(foo/2 :: ((integer(), atom()) -> integer())).

You can also specify the variable names of your function to simplify 
additional comments

-spec(foo/2 :: ((X :: integer(), Y :: atom()) -> integer()).

Also, you can use type variables in the type position.

-spec(bar/1 :: ((X :: Alpha) -> Alpha)).

You can put bounds on your type variables.

-spec(bar/1 :: ((X :: Alpha) -> Alpha) when is_subtype(Alpha,number())).

Also, to capture something similar to intersection types, you can give 
more than one contract

-spec(baz/1 :: ((integer() | atom()) -> integer());
                (([byte()]) -> atom)).

We also plan to have type aliases to keep the verbosity down a bit

-type(int_or_atom() :: (integer() | atom())).

Also, the contracts can involve typed records

-record(foo, {bar::integer(), baz}).

which then are used as

-spec(gazonk/1 :: ((#foo{}) -> integer())).


This is the syntax we are currently using, but it is not set in stone. 
Comments are welcome.

Tobias



More information about the erlang-questions mailing list