[erlang-questions] More general record syntax?

Garrett Smith g@REDACTED
Thu Dec 24 00:23:41 CET 2009


On Wed, Dec 23, 2009 at 11:49 AM, Dowse, Malcolm <malcolm@REDACTED> wrote:

-snip-

> My idea was to allow two different atoms for the two uses. The syntax
> could be something like this:
>
> #compile_time_atom/run_time_atom{field1 = val1, field2 = val2, ...}

Ugh, and I thought it was painful to read *before* the enhancement ;)

> The main reason I'd find the above change useful is that when
> hot-upgrading a gen_server, the state record definition can change.
> Since you can't have two different record definitions (old and new) with
> the same name, you have to go mucking around with the internal tuple
> representation of the record.

You also run into this when Mnesia - any time you need to modify a
record definition, you have to go though a painful mucking process. I
haven't seen a particularly clean way to do this, so I'm curious what
others think here.

Personally, I'd like a way to convert tuples from one record def to
another. E.g.

  -record(foo, {field1, field2}).
  -record(foo_v1, {field1}).

  upgrade(Foo_v1) ->
      #foo(Foo_v1, foo_v1)

Since this would be a compile time conversion, I've introduced some
hackish "cast" syntax. Not pretty, but you get the idea -- I want a
foo record and I provide a tuple that complies with the foo_v1
definition. The first item in the tuple would be ignored since we're
interested in mapping field values to tuple locations and don't care
about the original record type.

This doesn't solve pattern matching, but you can always include a
version field in your record defs. It has the advantage of providing a
trivial way to transform record tuples.

Garrett


More information about the erlang-questions mailing list