tags:

views:

136

answers:

3

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

+4  A: 

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

Zed
Ah yes, of course, because they are a compile time feature. Probably not a good idea to use records for a client API then?
Zubair
No, I think using records is good while you are using correct version of .hrl file. It is widely used in third party modules and applications and also in some core modules.
Hynek -Pichi- Vychodil
+2  A: 

I think the biggest downside is that it's not idiomatic. Have you ever seen an API that required you to construct a record and pass it in?

Why would you want to do something that's going to feel foreign to any erlang programmer? There's a convention already in use for optional named arguments to functions. Inventing yet another way without good cause is pointless.

Dustin
As long as the return value of a function is a record, why couldn't it be used as an input for another?
Zed
+2  A: 

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.

Amadiro