Erlang Mailing Lists

Author Message

<  Erlang  ~  variable record field selection

mahandler
Posted: Wed Apr 23, 2008 10:31 pm Reply with quote
Joined: 23 Apr 2008 Posts: 5
I have a record that looks like this:

-record(address, {f1, f2, ...}).

and i want to make a function so that i can iterate one of the fields of the #address record as such:

iterate(Address, FieldNumber) ->...

where FieldNumber = f1 | f2 | etc... = atomic()

This causes problems because Erlang won't let me do

Address#address{FieldNumber = SomeValue}
or
SomeValue = Address#address.FieldNumber + 1

any suggestions? am I formulating this wrong?

Obviously there are workarounds, but for simplicity and future reference I would like to know if there is a general way to use a variable to address a specific element of a record

Thanks in advance
View user's profile Send private message
rvirding
Posted: Fri Apr 25, 2008 10:55 pm Reply with quote
User Joined: 30 Aug 2006 Posts: 452 Location: Stockholm, Sweden
There is nothing which directly does what you want. If I have understood you correctly. You always have to explicitly give the field name.

Two alternatives off the top of my head are:

- If you know the field names you can use the #rec.field construct which returns the index into the record tuple of that field. With given name though.

- There is an automatically generated pseudo-function record_info(fields, Record) which returns a list of the fields (names) for that record. In order IIRC. And you know that the first field has index 2, the first element is the record name.

This would allow you to iterate over the fields.

Look in the records section of the on-line manual.

Robert
View user's profile Send private message Visit poster's website MSN Messenger
mahandler
Posted: Wed Apr 30, 2008 4:15 pm Reply with quote
Joined: 23 Apr 2008 Posts: 5
the problem is that I want to be able to change the record field names or add new ones in the future and not have this cause problems.

essentially i want a function like

extract_field_from_record(Record, FieldName)

with Record -> some version of a record
FieldName -> atom representing name of field to be extracted

however, it looks like maybe i can construct this function to work with the record_info(fields, Record) function... i'll try this
View user's profile Send private message
mahandler
Posted: Wed Apr 30, 2008 4:31 pm Reply with quote
Joined: 23 Apr 2008 Posts: 5
found this: http://www.erlang.org/pipermail/erlang-questions/2007-January/024665.html

Quote:
> record_info is not a proper function. It only exists during compilation,
> which means that it cannot take variable arguments.
>
> record_info(fields, address) %% works
>
> Rec = address,
> record_info(fields, Rec) %% doesn't work!
>
> Of course it only works if the address record is defined in that module.
>
> If you do want a function that uses record_info, try to use a macro
> instead.
>
> This will work:
>
> -define(print_fields(Rec), io:format("~p~n", [record_info(fields, Rec)])).
>
> And then use: ?print_fields(address)
>
> This will not work:
>
> print_fields(Rec) ->
> io:format("~p~n", [record_info(fields, Rec)]).
View user's profile Send private message

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