record field names

Ulf Wiger (AL/EAB) ulf.wiger@REDACTED
Wed Sep 28 10:51:07 CEST 2005


Here's a diff for sys_pre_expand.erl (rel. to compiler-4.3.9),
which converts the pseudo-function record_info/2 into a real,
exported function.

I decided that I really would like to have this function
myself.

Example:

ws12858> erlc -W sys_pre_expand.erl
./sys_pre_expand.erl:617: Warning: erlang:hash/2 obsolete; use erlang:phash2/2
./sys_pre_expand.erl:626: Warning: erlang:hash/2 obsolete; use erlang:phash2/2
ws12858> erlc -pa . ../classic_server.erl
ws12858> erl -pa .
Erlang (BEAM) emulator version 5.4.9 [hipe] [threads:0] [kernel-poll]

Eshell V5.4.9  (abort with ^G)
1> classic_server:module_info(records).
[state]
2> classic_server:record_info(fields,state).
[value]
3> classic_server:record_info(size,state).  
2
4> classic_server:record_info(dummy,state).

** exited: {function_clause,[{classic_server,record_info,[dummy,state]},
                             {erl_eval,do_apply,5},
                             {shell,exprs,6},
                             {shell,eval_loop,3}]} **



/Uffe



> -----Original Message-----
> From: owner-erlang-questions@REDACTED
> [mailto:owner-erlang-questions@REDACTED]On Behalf Of Ulf Wiger
> (AL/EAB)
> Sent: den 28 september 2005 09:30
> To: Mats Cronqvist (AL/EAB)
> Cc: erlang-questions@REDACTED
> Subject: RE: record field names
> 
> 
> 
> 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:mats.cronqvist@REDACTED]
> > Sent: den 16 september 2005 17:05
> > To: Ulf Wiger (AL/EAB)
> > Cc: erlang-questions@REDACTED
> > 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: owner-erlang-questions@REDACTED
> > >>[mailto:owner-erlang-questions@REDACTED]On Behalf Of Mats 
> > Cronqvist
> > >>Sent: den 16 september 2005 16:22
> > >>To: erlang-questions@REDACTED
> > >>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
> > >>
> > 
> 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: sys_pre_expand.erl.diff
Type: application/octet-stream
Size: 5481 bytes
Desc: sys_pre_expand.erl.diff
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20050928/a423609f/attachment.obj>


More information about the erlang-questions mailing list