Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any downsides to passing in an Erlang record as a function argument?

Tags:

erlang

record

Are there any downsides to passing in an Erlang record as a function argument?

like image 329
yazz.com Avatar asked Dec 12 '22 23:12

yazz.com


2 Answers

There is no downside, unless the caller function and the called function were compiled with different 'versions' of the record.

like image 199
Zed Avatar answered Apr 27 '23 18:04

Zed


Some functions from erlangs standard library do indeed use records in their interfaces (I can't recall which ones, right now--but there are a few), but in my humble opinion, the major turnoff is, that the user will have to include your header file, just to use your function.

That seems un-erlangy to me (you don't ever do that normally, unless you're using said functions from the stdlib), creates weird inter-dependencies, and is harder to use from the shell (I wouldn't know from the top of my head how to load & use records from the shell -- I usually just "cheat" by constructing the tuple manually...)

Also, handling records is a bit different from the stuff you usually do, since their keys per default take the atom 'undefined' as value, au contraire to how you usually do it with proplists, for instance (a value that wasn't set just isn't there) -- this might cause some confusion for people who do not normally work a lot with records.

So, all-in-all, I'd usually prefer a proplist or something similar, unless I have a very good reason to use a record. I do usually use records, though, for internal state of for example a gen_server or a gen_fsm; It's somewhat easier to update that way.

like image 38
Jonathan Ringstad Avatar answered Apr 27 '23 18:04

Jonathan Ringstad