[erlang-questions] edoc, erlc and dialyser type spec consistency

David Mercer dmercer@REDACTED
Fri Nov 20 20:31:22 CET 2009


Kostis Sagonas wrote:

> You managed to confuse me here... How can you have a "reusable" funtype?
> You cannot have functions with the same arguments in an Erlang module.
> Can you give us a concrete example of what you want to achieve and you
> cannot?

Let's say I am writing a parsing combinator library.  I define a parser as:

	-type parser(A) :: fun((state(A)) -> parse_result(A)).

The parameter is, say, the type of the tokens or something.  So a parser
that parses characters might have the following type:

	-type char_parser() :: parser(char()).

Now I have a combinator that given two parser()'s, creates a parser that
parses the first parser() followed by the second:

	-spec seq(parser(A), parser(A)) -> parser(A).

That's good, until I want to start defining parsers.  Every parser must
share the same specification, as laid out by parser():

	-spec parse_xml_tag(state(char())) -> parse_result(char()).
	-spec parse_edi_segment(state(char())) -> parse_result(char()).
	-spec parse_whitespace(state(char())) -> parse_result(char()).

And so on.  What I really want to say is that these functions are in
accordance with the type specification of parser(_) (or, in this case,
subtype char_parser()):

	-spec parse_xml_tag :: char_parser().
	-spec parse_edi_segment :: char_parser().
	-spec parse_whitespace :: char_parser().

That conveys my intent better, as a reader does not have to compare the
function specs to the parser() type to see if he can pass it to seq/2 or
not.

Did that make sense?

Cheers,

David



More information about the erlang-questions mailing list