I need function which returns true, when record has certain field and vice versa. Example:
-record(robot, {name,
type=industrial,
hobbies,
details=[]
}).
Crusher = #robot{name="Crusher", hobbies=["Crushing people","petting cats"]}.
SomeFunction(Crusher, hobbies). %% returns true
SomeFunction(Crusher, fantasy). %% returns false
Sorry for my English. It was exercise in Functional Programming in my University. It was written for Lisp, where is simple solution. But for me it was required by Erlang. thank you very much for your help. I wrote some ugly function. It is not solution for my first question. It is enough for my teacher.
searchArray(_, []) -> false;
searchArray(X, [X|_]) -> true;
searchArray(X, [_|T]) -> searchArray(X, T).
existField(Field) ->
searchArray(Field, record_info(fields, robot)).
And I think, too, maps is much more usefull here.
You can use record_info(fields, Record) to get list of record fields. You need to know that records and this function is a little bit tricky. Records are translated to fixed size tuple during compilation and pseudo function record_info is added.
#robot{name="Crusher", hobbies=["Crushing people","petting cats"]}
becomes:
{robot, "Crusher", industrial, ["Crushing people","petting cats"], []}
If you want to share record you should put it in .hrl file and include it in each module you want to be able to construct it using "#record_name{}", extract elements with dot operator by field or use record_info.
Since release 17 we have maps which should work for you as well and won't damage your brain.