Erlang Mailing Lists

Author Message

<  Erlang questions mailing list  ~  record_info/2

mikl at linux-france.org
Posted: Thu Jun 24, 1999 4:52 pm Reply with quote
Guest
Hi,

I get a compile time error message when trying to use record_info/2 outside of the 'mnesia:create_table' context.

The error message is :
illegal record info

What I am trying to do is get the list of fields of an object :
(The aim of the function is to replace a fields value by its name, without specifically knowing what kind of record I am manipulating)

replace(Attr,Value, Obj) ->
%% Get record_name from Object
Record_name = element(1,Obj),
%% Get list of fields
List = record_info(fields, Record_name),
%% Get attribute position
%% +1 because of the table name at first position
Pos = pos(Attr,List) + 1,
case Pos of
0 ->
{error, attribute_does_not_exist_in_record};
N ->
Result = setelement(Pos, Obj, Value),
{ok,Result}
end.


Can you figure out what I am doing wrong or what I am misunderstanding ?


Thanks in advance for your help.

--
Mickael Remond
mikl_at_linux-france.org
ICQ : 2265396


Post generated using Mail2Forum (http://m2f.sourceforge.net)
tobbe at serc.rmit.edu.au
Posted: Thu Jun 24, 1999 7:06 pm Reply with quote
Guest
> What I am trying to do is get the list of fields of an object :
> (The aim of the function is to replace a fields value by its name,
> without specifically knowing what kind of record I am manipulating)

> Record_name = element(1,Obj),
> %% Get list of fields
> List = record_info(fields, Record_name),

record_info/2 is resolved at compile time, so you can't do the above
(Remember, records is just syntactic sugar on top of tuples)

/Tobbe




Post generated using Mail2Forum (http://m2f.sourceforge.net)
hakan at erix.ericsson.se
Posted: Mon Jun 28, 1999 5:51 am Reply with quote
Guest
On Fri, 25 Jun 1999, Torbjorn Tornkvist wrote:

>Date: Fri, 25 Jun 1999 05:05:53 +1000
>From: Torbjorn Tornkvist <tobbe_at_serc.rmit.edu.au>
>To: Mickael Remond <mikl_at_linux-france.org>
>Cc: erlang-questions_at_erlang.org
>Subject: Re: record_info/2
>
>
>> What I am trying to do is get the list of fields of an object :
>> (The aim of the function is to replace a fields value by its name,
>> without specifically knowing what kind of record I am manipulating)
>
>> Record_name = element(1,Obj),
>> %% Get list of fields
>> List = record_info(fields, Record_name),
>
>record_info/2 is resolved at compile time, so you can't do the above
>(Remember, records is just syntactic sugar on top of tuples)

If the table happens to be a Mnesia table, you may use the function
mnesia:table_info/2 instead of record_info/2:

.
.
TableName = element(1, Obj),
FieldList = mnesia:table_info(TableName, attributes),
.
.

mnesia:table_info/2 is resolved in run time.

/H

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum