[erlang-questions] How to access functions type signatures?

Joseph Wayne Norton <>
Wed Jul 30 16:12:08 CEST 2008


Dimitry -

If edoc specs are available, edoc can be used for this purpose.  The  
outputed xml can be parsed.  There might be other (or better approaches).


%% @spec () -> ok | whatever
%% @doc Helper function to make it easier to generate XML Edoc
%% documentation
%%

make_xml() ->
     make_xml([undefined]).

%% @spec ([atom()]) -> ok | whatever
%% @doc Helper function to make it easier to generate XML Edoc
%% documentation
%%

make_xml([ApplicationName]) ->
     {ok, CurrentDir} = file:get_cwd(),

     file:set_cwd(".."),
     {ok, Up1Path} = file:get_cwd(),
     LastDir = filename:basename(Up1Path),
     file:set_cwd(".."),
     Options = [{new, true}, {hidden, true}, {private, true}, {todo, true}]
         ++ [{layout, ?MODULE}, {dir, LastDir ++ "/doc/xml"}, {file_suffix,  
".xml"}],
     Res =
         if ApplicationName =/= undefined ->
                 edoc:application(atom_ify(ApplicationName), LastDir,  
Options);
            true ->
                 edoc:application(atom_ify(LastDir), LastDir, Options)
         end,

     file:set_cwd(CurrentDir),
     Res.

%% ---------------------------------------------------------------------

%% @spec atom_ify(X::term()) -> atom()
%% @doc Convert a term to an atom (if convertable).

atom_ify(X) when is_atom(X) ->
     X;
atom_ify(X) when is_binary(X) ->
     list_to_atom(binary_to_list(X));
atom_ify(X) when is_list(X) ->
     list_to_atom(X).

%% ---------------------------------------------------------------------
module(#xmlElement{name = module, content = Es}, _Options) ->
     Prolog = ["<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"],
     Content = [#xmlElement{
       name=package
       , attributes=[#xmlAttribute{name=name,value=[]}]
       , content=[#xmlElement{
           name=modules
           , attributes=[]
           , content=[#xmlElement{
               name=module
               , attributes=[#xmlAttribute{name=name,value=[]}]
               , content=Es
              }]}]}],
     xmerl:export_simple(Content,xmerl_xml,[{prolog,Prolog}]).

package(#xmlElement{name = package, content = Es}, _Options) ->
     Prolog = ["<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"],
     xmerl:export_simple(Es,xmerl_xml, [{prolog,Prolog}]).

overview(#xmlElement{name = overview, content = Es}, _Options) ->
     Prolog = ["<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"],
     xmerl:export_simple(Es,xmerl_xml, [{prolog,Prolog}]).




On Wed, 30 Jul 2008 22:56:29 +0900, Dimitry Golubovsky  
<> wrote:

> Hi,
>
> If somewhere in the documentation we see:
>
> fread([IoDevice,] Prompt, Format) -> Result
>
>
> Types:
>
> IoDevice = io_device()
> Prompt = atom() | string()
> Format = string()
> Result = {ok, Terms} | eof | {error, What}
>  Terms = [term()]
>  What = term()
>
>
> is the above type information stored anywhere in a machine-consumable
> form (being readable by an Erlang program is fine)?
>
> I am asking this in connection with my Haskell-to-Erlang conversion
> experiments. Being able to access such information, it would be
> possible to create Haskell type signatures to call these functions
> from Haskell code, something like this (manually derived):
>
> fread :: IoDevice -> Prompt -> Format -> Result -- or rather IO Result
>
> data IoDevice = IoDevice -- perhaps an opaque type
>
> data Term = Term -- another opaque type
>
> type Prompt = String
> type Format = String
> data Result = <some algebraic data type>
>
> etc.
>
> I did something like this earlier for HSFFIG (derived foreign imports
> from C function prototypes), but I had to parse C headers, which I
> would like to avoid this time.
>
> Thanks.
>
> PS this must be somewhere in th docs, but I haven't been able to dig it  
> up.
>



-- 




More information about the erlang-questions mailing list