Yeti (was Re: Small Erlang bencode library)
Thu May 18 10:36:40 CEST 2006
--- Steve Smith <> wrote:
> Has anyone ever looked at adding stronger typing to
> Erlang (*strong*,
> not static :), eg. the ability to define new types
> that are checkable at
Yes. I've written a system, Yeti, which permits you to
define complex types that are checked at runtime.
Definitions are written down inside your module and
look a bit like those in the Erlang man pages, a bit
like EDoc, and a bit like Python decorators:
%% io_list() is a list of bytes, binaries and
%% other io_lists.
@type io_list() ::= [(byte() | binary() | io_list())].
%% foo/1 takes an io_list() as argument, returns
%% unknown type.
@spec foo(io_list()) -> @.
Note that these types are both more and less general
than ordinary polymorphic, algebraic types.
These definitions are turned into two things:
- foo/1 is decorated with type checks on input and
- io_list() is made accessible via a new function
So foo/1 checks its input. But you can also perform an
ad hoc type check like this:
5> my_module:type_info(io_list, X).
This sort of type checking can be handy, but can of
course also have a great deal of overhead when data
are being checked repeatedly. Yeti optimizes the
generated type checking functions already, and I think
one can reduce the cost at compile-time with a bit of
cleverness. But the general problem is likely to
A benefit I didn't see when I started: it can be quite
useful just to have a library of "types" available
when debugging or even developing.
A nice future extension might be to permit calls to
type checking functions in guards:
@type string() ::= [byte()].
foo(X) when string(X) -> ...;
foo(a) -> ...;
Yeti has a number of other features too, but "this
message is too short to describe them" :-)
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
More information about the erlang-questions