[erlang-questions] : Use of Records in the Java API

Raimo Niskanen <>
Wed Jun 11 13:31:37 CEST 2008

On Wed, Jun 11, 2008 at 12:58:57PM +0200, Gleb Peregud wrote:
> On Wed, Jun 11, 2008 at 10:23 AM, Alexander Lamb
> <> wrote:
> > Hello,
> >
> > My Erlang modules are mainly a server to Java Web applications (for
> > the time being).
> >
> > This means that I am making a big use of the Erlang Java API which
> > works fine.
> > Since my Erlang modules use Mnesia to store data coming from the Java
> > side (and vice versa), I need to implement functions with many
> > parameters for example to insert new data in a Mnesia table.
> > I also need to convert tuples into actual Java objects when reading
> > data.
> >
> > This would be simpler if the Java API had an OtpErlangRecord. It would
> > make it possible to subclass it and could even be the prefered way of
> > bridging the Erlang world with the Java world. It would also avoid
> > breaking code each time a new attribute is needed for a Mnesia table
> > (making it today necessary to add a parameter to the complete chain of
> > function calls).
> >
> > I didn't find any mention of OtpErlangRecord in the documentation. I
> > suspect it is because the Java API was written before records were
> > introduced in Erlang.
> >
> > In a more general sense, is there any plan to enhance the
> > functionalities of the Java API  to handle more gracefully the
> > mismatch between the loosely typed functional world of Erlang and the
> > object oriented world of Java?
> >
> > I could very well imagine a Java class to handle marshalling and
> > unmarshalling of terms at a high level using records. You would add a
> > type description to the attributes and on the Java side you would
> > immediately receive objects (subclasses of OtpErlangRecord for
> > example). It could look somewhat like tools such as Cayenne or
> > Hibernate in the SQL world.
> >
> > Just some thoughts. And since in two weeks Dennis Byrne is making a
> > presentation at Erlang Exchange on that subject, maybe if Dennis is
> > reading this post, he could consider preparing a few words in that
> > direction for his London speech:-)
> > That comment actually also applies if the client is in ActionScript
> > for a Flash/Flex application or even JavaScript.
> >
> > Alex
> Hello,
> There's a problem with records on the Java side, since records are
> comple-time module-specific features. As far i know erlang VM does not
> have any runtime information about records, hence it is unable to send
> it to Java side. Though probably it is not possible to implement
> OtpErlangRecord in general way.
> Feel free to correct me if i'm wrong.

8.7 Internal Representation of Records

Record expressions are translated to tuple expressions during compilation. A record defined as

-record(Name, {Field1,...,FieldN}).

is internally represented by the tuple


where each ValueI is the default value for FieldI.

To each module using records, a pseudo function is added during compilation to obtain information about records:

record_info(fields, Record) -> [Field]
record_info(size, Record) -> Size

Size is the size of the tuple representation, that is one more than the number of fields. 

> Best regards,
> -- 
> Gleb Peregud
> http://gleber.pl/
> Every minute is to be grasped.
> Time waits for nobody.
> -- Inscription on a Zen Gong
> _______________________________________________
> erlang-questions mailing list
> http://www.erlang.org/mailman/listinfo/erlang-questions


/ Raimo Niskanen, Erlang/OTP, Ericsson AB

More information about the erlang-questions mailing list