record field names

Ulf Wiger (AL/EAB) <>
Wed Sep 28 09:29:50 CEST 2005


Interestingly, in sys_pre_expand.erl, you find the 
following code and comment:

%% Expand a call to record_info/2. We have checked that it is not
%% shadowed by an import.

record_info_call(Line, [{atom,_Li,Info},{atom,_Ln,Name}], St) ->
    case Info of
        size ->
            {{integer,Line,1+length(record_fields(Name, St))},St};
        fields ->
            {make_list(field_names(record_fields(Name, St)), Line),St}
    end.

Apparently, someone had envisioned a way to import a
record_info/2 function. From where, one might wonder?

/Uffe

> -----Original Message-----
> From: Mats Cronqvist [mailto:]
> Sent: den 16 september 2005 17:05
> To: Ulf Wiger (AL/EAB)
> Cc: 
> Subject: Re: record field names
> 
> 
>    name clashes? surely you can't have a real record_info/2 function?
> 
>    mats
> 
> Ulf Wiger (AL/EAB) wrote:
> > In order to eliminate name clashes, why not
> > 
> > Module:module_info({record, Rec, fields})
> > 
> > /Uffe
> > 
> > 
> >>-----Original Message-----
> >>From: 
> >>[mailto:]On Behalf Of Mats 
> Cronqvist
> >>Sent: den 16 september 2005 16:22
> >>To: 
> >>Subject: record field names
> >>
> >>
> >>   feels like this is an faq, but i can't find it in the archive.
> >>
> >>   according to the reference manual:
> >>   "To each module using records, a pseudo function is added 
> >>during compilation 
> >>to obtain information about records:
> >>record_info(fields, Record) -> [Field]
> >>record_info(size, Record) -> Size"
> >>
> >>   now, as i understand it, there is no function, just 
> >>textual substitution 
> >>(like a macro).
> >>
> >>   but why not? if there was a real, exported function 
> >>record_info/2 (just like 
> >>module_info), one could do a lot of cool things.
> >>
> >>  such as turning a record into a tagged tuple;
> >>
> >>readable_rec(Module,Rec) ->
> >>   
> >>lists:zip(Module:record_info(fields,element(1,Rec)),tl(tuple_t
> >>o_list(Rec))).
> >>
> >>  or write a tool to check that all modules using a certain 
> >>record agree on its 
> >>definition.
> >>
> >>   mats
> >>
> 



More information about the erlang-questions mailing list