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

Stefan Hellkvist hellkvist@REDACTED
Sun Jun 2 13:25:32 CEST 2019

> ...
> 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.

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

More information about the erlang-questions mailing list