<p>I am trying to refactor some code I have for software that collects current status of agents in a call queue. Currently, for each of the 6 or so events that I listen to, I check in a Mnesia table if an agent exists and change some values in the row depending on the event or add it as new if the agent doesn't exist. Currently I have this Mnesia transaction in each event and of course that is a bunch of repeated code for checking the existence of agents and so on. </p>

<p>I'm trying to change it so that there is one function like <em>change_agent/2</em> that I call from the events that handles this for me. </p>

<p>My problems are of course records.... I find no way of dynamically creating them or merging 2 of them together or anything. Preferably there would be a function I could call like:</p>

<pre><code>change_agent("001", #agent(id = "001", name = "Steve")).
change_agent("001", #agent(id = "001", paused = 0, talking_to = "None")).
</code></pre>

[{'answer_id': 62556, 'author': 'uwiger', 'author_id': 6834, 'author_profile': 'https://Stackoverflow.com/users/6834', 'pm_score': 2, 'selected': False, 'text': '<p>It is difficult to write generic access functions for records.\nOne workaround for this is the <a href="http://forum.trapexit.org/viewtopic.php?p=21790#21790" rel="nofollow noreferrer">\'exprecs\'</a> library, which\nwill generate code for low-level record access functions.</p>\n\n<p>The thing you need to do is to add the following lines to \na module:</p>\n\n<pre><code>-compile({parse_transform, exprecs}).\n-export_records([...]).  % name the records that you want to \'export\'\n</code></pre>\n\n<p>The naming convention for the access functions may look strange, but was inspired by a proposal from Richard O\'Keefe. It is, at least, consistent, and unlikely to clash with existing functions. (:</p>\n'}, {'answer_id': 62898, 'author': 'Adam Lindberg', 'author_id': 2457, 'author_profile': 'https://Stackoverflow.com/users/2457', 'pm_score': 3, 'selected': True, 'text': '<p>I wrote some code a while ago that merges two records. Is not entirely dynamic, but whith macros you could easily use it for several records.</p>\n\n<p>It works like this: The merge/2 function takes two records and converts them to lists together with the empty record for reference (the record type is defined at compile time, and must be. This is the "undynamic" part). These are then run through the generic function merge/4 which works with lists and takes elements from A if they are defined, otherwise from B if they are defined, or lastly from Default (which is always defined).</p>\n\n<p>Here\'s the code (please excuse StackOverflow\'s poor Erlang syntax highlighting):</p>\n\n<pre><code>%%%----------------------------------------------------------------------------\n%%% @spec merge(RecordA, RecordB) -&gt; #my_record{}\n%%%     RecordA = #my_record{}\n%%%     RecordB = #my_record{}\n%%%\n%%% @doc Merges two #my_record{} instances. The first takes precedence.\n%%% @end\n%%%----------------------------------------------------------------------------\nmerge(RecordA, RecordB) when is_record(RecordA, my_record),\n                             is_record(RecordB, my_record) -&gt;\n    list_to_tuple(\n        lists:append([my_record],\n                     merge(tl(tuple_to_list(RecordA)),\n                           tl(tuple_to_list(RecordB)),\n                           tl(tuple_to_list(#my_record{})),\n                           []))).\n\n%%%----------------------------------------------------------------------------\n%%% @spec merge(A, B, Default, []) -&gt; [term()]\n%%%     A = [term()]\n%%%     B = [term()]\n%%%     Default = [term()]\n%%%\n%%% @doc Merges the lists `A\' and `B\' into to a new list taking\n%%% default values from `Default\'.\n%%%\n%%% Each element of `A\' and `B\' are compared against the elements in\n%%% `Default\'. If they match the default, the default is used. If one\n%%% of them differs from the other and the default value, that element is\n%%% chosen. If both differs, the element from `A\' is chosen.\n%%% @end\n%%%----------------------------------------------------------------------------\nmerge([D|ATail], [D|BTail], [D|DTail], To) -&gt;\n    merge(ATail, BTail, DTail, [D|To]); % If default, take from D\nmerge([D|ATail], [B|BTail], [D|DTail], To) -&gt;\n    merge(ATail, BTail, DTail, [B|To]); % If only A default, take from B\nmerge([A|ATail], [_|BTail], [_|DTail], To) -&gt;\n    merge(ATail, BTail, DTail, [A|To]); % Otherwise take from A\nmerge([],        [],        [],        To) -&gt;\n    lists:reverse(To).\n</code></pre>\n\n<p>Feel free to use it in any way you want.</p>\n'}]
['https://Stackoverflow.com/questions/62245', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5601/']