[erlang-questions] Why use -record() ?

bengt cean.ebengt@REDACTED
Sun Jun 2 13:34:57 CEST 2019


It is true that it is a matter of taste but remember that some Erlang containers can not be used to pattern match. Using one of those handle_call/3 would only be one function clause. And while maps can be pattern matched the compiler will not help you with spelling mistakes, as it does with records.

Best Wishes,

> On 2 Jun 2019, at 13:25, Stefan Hellkvist <hellkvist@REDACTED <mailto:hellkvist@REDACTED>> wrote:
>> ...
>> handle_call({run, Args}, _From, {Limit, Spv, Refs, Queue}) when N > 0 ->
>>   {ok, Pid} = supervisor:start_child(Spv, Args),
>>   Ref = erlang:monitor(process, Pid),
>>   NewRefs = gb_sets:add(Ref, Refs),
>>   NewState = {Limit-1, Spv, NewRefs, Queue},
>>   {reply, {ok, Pid}, NewState};
>> We also can use the above alternative code right?
> Yes, you can use whatever Erlang term you prefer - record, tuple, map, a list of maps containing records of tuples...
> What you choose is a matter of taste and depends on your requirements. A record for instance has the advantage over a tuple, that you access elements by name and the order of elements therefore becomes irrelevant. This could give advantages if you one day decide to add another field to your state. With the tuple approach this would likely break every access to the tuple everywhere in the code, but with a record where you access and match by names most of your code might still work...plus you never need to remember what meaning the third or fourth element in your tuple has l, because it has a descriptive name.
> Stefan 
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED <mailto:erlang-questions@REDACTED>
> http://erlang.org/mailman/listinfo/erlang-questions

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20190602/6f4f264b/attachment.htm>

More information about the erlang-questions mailing list