[erlang-questions] Accessing 'spec' information at runtime

Michael Truog <>
Tue Mar 8 18:05:49 CET 2011


There is already code for this:
https://github.com/okeuday/CloudI/blob/master/src/lib/unused/src/abstract_code.erl

An example is:
2> test:function(a).
[{attribute,1,file,{"./test.erl",1}},
 {attribute,1,module,test},
 {attribute,4,export,[{function,1}]},
 {attribute,6,spec,
            {{function,1},
             [{type,6,'fun',
                    [{type,6,product,
                           [{ann_type,6,[{var,6,'A'},{type,6,any,[]}]}]},
                     {type,6,any,[]}]}]}},
 {function,7,function,1,
           [{clause,7,
                    [{var,7,'A'}],
                    [],
                    [{call,8,
                           {remote,8,{atom,8,io},{atom,8,format}},
                           [{string,8,"~p~n"},
                            {cons,8,
                                  {call,8,{atom,8,'$abstract_code$'},[]},
                                  {nil,8}}]},
                     {var,9,'A'}]}]},
 {eof,10}]

For the code:
-module(test).
-compile({parse_transform, abstract_code}).

-export([function/1]).

-spec function(A::any()) -> any().
function(A) ->
   io:format("~p~n", ['$abstract_code$'()]),
   A.

On 03/08/2011 08:00 AM, Eric Merritt wrote:
> I think that is an extraordinarily good idea Kenneth. Having that
> information be a first class chunk would be useful.
>
> How do record type specs appear. I suspect they are part of the
> abstract code for the record.
>
> On Tue, Mar 8, 2011 at 9:48 AM, Kenneth Lundin <> wrote:
>> The -spec and -type is included in the abstract_code chunk in the beam
>> files. If
>> they are compiled to contain debug_info (erlc +debug_info my_module.erl).
>>
>> The appear as attributes and the can be abstracted like this (only the
>> -spec(s) in this example):
>>
>> %% Beam is a string contaning the path to a beam file i.e
>> "/home/user/mymodule.beam"
>> %% you can for example call it like this from the shell:
>> %% m:getspecs(code:which(lists)).
>>
>> getspecs(Beam)->
>>        {ok,{_,[{abstract_code,{_,AC}}]}} =
>> beam_lib:chunks(Beam,[abstract_code]),
>>    Specs = [Spec|| Spec = {attribute,_,'spec',{FA,_}} <- AC],
>>    io:fwrite("~s~n", [erl_prettypr:format(erl_syntax:form_list(Specs))]).
>>
>> Hmm, just thinking. Maybe it would be a good idea to have the -spec
>> info available as a separate
>> chunk who is always present even without debug_info.
>> The -spec info could for example be used for a smarter tab-completion
>> of functions in the shell.
>> Including argument names and types.
>>
>> /Kenneth, Erlang/OTP Ericsson
>>
>> On Tue, Mar 8, 2011 at 3:41 PM, Eric Merritt <> wrote:
>>> Hello All,
>>>
>>>
>>>  I have been doing some digging and it I haven't found anything that
>>> indicates that spec information survives compilation. It may in the
>>> beam files, but there isn't any documentation to support that
>>> assumption in beam_lib. So before I go digging in the source I thought
>>> I would ping the list and see if anyone has any ideas on accessing
>>> 'speced' type information at run time.
>>>
>>> Thanks,
>>> Eric
>>>
>>> ________________________________________________________________
>>> erlang-questions (at) erlang.org mailing list.
>>> See http://www.erlang.org/faq.html
>>> To unsubscribe; mailto:
>>>
>>>
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:
>
>



More information about the erlang-questions mailing list