[erlang-questions] More general record syntax?

Zoltan Lajos Kis kiszl@REDACTED
Thu Dec 24 00:39:51 CET 2009


I would be lying if I said I understood Malcolm's proposal :)
My only problem with records is that they are not versioned. That could 
simply be solved by adding a version field to records. For example:

definition:   -record(rec, "0.1", {field1 = a, field2 = b}).
referral:   #rec("0.1"){field1 = F}   (if there is only one version of 
the record in the given context, the version could be omitted)
representation:   {rec, "0.1", a, b}

This way code change could look like:

code_change(_OldVsn, #rec("0.1"){a = A, b = B}, _Extra) ->
  {noreply, #rec("0.2"){a = A, b = B, c = 0}}.

and would even work for upgrading from unversioned to versioned records.

Regards,
Zoltan.


Garrett Smith wrote:
> 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
>
> ________________________________________________________________
> erlang-questions mailing list. See http://www.erlang.org/faq.html
> erlang-questions (at) erlang.org
>
>   



More information about the erlang-questions mailing list