Yeti (was Re: Small Erlang bencode library)
Thomas Lindgren
thomasl_erlang@REDACTED
Thu May 18 10:36:40 CEST 2006
--- Steve Smith <ssmith@REDACTED> 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
> runtime?
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
output
- io_list() is made accessible via a new function
family, type_info/N
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).
false
6>
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
remain.
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" :-)
Best,
Thomas
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
More information about the erlang-questions
mailing list