[erlang-questions] Why are records not 'export'-able?

274 views
Skip to first unread message

Mahesh Paolini-Subramanya

unread,
Jun 29, 2012, 10:06:53 AM6/29/12
to Erlang Questions
If I have a record I use in just one module, I declare it in that module.
If I have a record I use across a number of modules, I declare it in an included file.
If I have a record that I use across a number of applications, I end up declaring it once in each application.

The simple-minded me says "Hmf. If I could just have a '-export_record' declaration, then I could do some fun things like '-record(Name, Application:Record)'

The more realistic me says "There is probably some reason why you can't do this. Probably has to do with compilers. It usually does".

Anyone?

That Tall Bald Indian Guy...
Blog   |   Twitter   |   Google+

Ladislav Lenart

unread,
Jun 29, 2012, 10:27:56 AM6/29/12
to Mahesh Paolini-Subramanya, Erlang Questions
Hello.

You can create a library application with your include file (and perhaps other
common modules) and then use -include_lib(...) from all other applications.


HTH,

Ladislav Lenart


On 29.6.2012 16:06, Mahesh Paolini-Subramanya wrote:
> If I have a record I use in just one module, I declare it in that module.
> If I have a record I use across a number of modules, I declare it in an
> included file.
> If I have a record that I use across a number of applications, I end up
> declaring it once in each application.
>
> The simple-minded me says "Hmf. If I could just have a '-export_record'
> declaration, then I could do some fun things like '-record(Name,
> Application:Record)'
>
> The more realistic me says "There is probably some reason why you can't do this.
> Probably has to do with compilers. It usually does".
>
> Anyone?
>
> */
> */Mahesh Paolini-Subramanya/*
> <http://www.gravatar.com/avatar/204a87f81a0d9764c1f3364f53e8facf.png>
> That Tall Bald Indian Guy...
> Blog <http://dieswaytoofast.blogspot.com/> | Twitter
> <https://twitter.com/dieswaytoofast> | Google+
> <https://plus.google.com/u/0/108074935470209044442/posts>
> /*
> __
>
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-q...@erlang.org
> http://erlang.org/mailman/listinfo/erlang-questions


_______________________________________________
erlang-questions mailing list
erlang-q...@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions

Robert Virding

unread,
Jun 29, 2012, 11:27:10 AM6/29/12
to Mahesh Paolini-Subramanya, Erlang Questions
The main reason is that records are purely compile-time and record definitions only exist in the compiler. At runtime a record is just a normal tuple with nothing in it to show that it came from a record or what the record definition is. So there are no problems accessing record as tuples and tuples as records, though in most cases it is a stupid thing to do.

So the problem is that you need to be able to access the record definition whenever a module using it is compiled. One solution is to put the record definition .hrl file in an applications include directory. It is then possible to access that file from the compiler using:

-include_lib("app/include/recdef.hrl").

The compiler will extend the app name with version numbers when necessary.

Robert


Attila Rajmund Nohl

unread,
Jun 29, 2012, 11:27:21 AM6/29/12
to erlang-questions
2012/6/29 Mahesh Paolini-Subramanya <mah...@dieswaytoofast.com>:
> If I have a record I use in just one module, I declare it in that module.
> If I have a record I use across a number of modules, I declare it in an
> included file.
> If I have a record that I use across a number of applications, I end up
> declaring it once in each application.
>
> The simple-minded me says "Hmf. If I could just have a '-export_record'
> declaration, then I could do some fun things like '-record(Name,
> Application:Record)'
>
> The more realistic me says "There is probably some reason why you can't do
> this. Probably has to do with compilers. It usually does".

As the documentation says, "record is not a true data type. Instead
record expressions are translated to tuple expressions during
compilation.", so there's nothing to export. However, nothing stops
you from including a header from an other application.

Mahesh Paolini-Subramanya

unread,
Jun 29, 2012, 12:08:56 PM6/29/12
to Erlang Questions
Sadly, I *knew* that records don't survive compilation - just didn't put the pieces together.

-include_lib is pretty much the most logical option here.  The only question remaining is "How much refactoring do I want to do?" :-)

Thanks all...

Cheers

That Tall Bald Indian Guy...
Blog   |   Twitter   |   Google+

Richard O'Keefe

unread,
Jul 1, 2012, 9:19:49 PM7/1/12
to Mahesh Paolini-Subramanya, Erlang Questions

On 30/06/2012, at 2:06 AM, Mahesh Paolini-Subramanya wrote:

> If I have a record I use in just one module, I declare it in that module.
> If I have a record I use across a number of modules, I declare it in an included file.
> If I have a record that I use across a number of applications, I end up declaring it once in each application.

Er, what stops you using the same -include file in more than one application?

If I found myself wanting to use the same -record in more than one
application, I'd lie down until the feeling wore off. More precisely,
I'd definitely want to hide that inside some sort of access module.
>
> The simple-minded me says "Hmf. If I could just have a '-export_record' declaration, then I could do some fun things like '-record(Name, Application:Record)'

But there is no _thing_ for it to bind to. Records are just a kind of macro
that the compiler understands.

With frames, there'd be no -record declaration in the first place, you'd just
use the data like you can just use a list or tuple.

With abstract patterns, you'd import them from the defining module the way
you'd import ordinary functions from their defining module.

Records are *neither* a data type *nor* something function-like.

Consider the following module:

-module(foo).
-export([bar/2]).

-record(ugh, {ugh}).

bar(i, #ugh{ugh=X}) -> #ugh{ugh=X+1};
bar(d, {ugh,Y}) -> {ugh,Y-1}.

To me, it looks like a very bad idea to use both record syntax
and plain tuple syntax for the same kind of data. I think that
if there is a -record declaration for a record name, and there
is also any tuple with the same name as its first element, the
compiler should warn about that. Currently it doesn't.

Mahesh Paolini-Subramanya

unread,
Jul 2, 2012, 11:23:14 AM7/2/12
to Richard O'Keefe, Erlang Questions

On 30/06/2012, at 2:06 AM, Mahesh Paolini-Subramanya wrote:

If I have a record I use in just one module, I declare it in that module.
If I have a record I use across a number of modules, I declare it in an included file.
If I have a record that I use across a number of applications, I end up declaring it once in each application.

Er, what stops you using the same -include file in more than one application?

If I found myself wanting to use the same -record in more than one
application, I'd lie down until the feeling wore off.  More precisely,
I'd definitely want to hide that inside some sort of access module.

Oh ouch :-)

Reply all
Reply to author
Forward
0 new messages