[erlang-questions] [ANN] Erlang/OTP 17.0-rc1 has been released.

Ulf Wiger <>
Mon Feb 3 10:35:21 CET 2014


On 1 Feb 2014, at 21:29, Max Lapshin <> wrote:

> Fred, your ideas are right, of course, but you forget that right now records doesn't have any infrastructure for code upgrade.
> 
> The only way is to make {state,  Field1, Field2, ....} mapping by hands.

Actually, there *is* a framework for this, although I can’t say if anyone actually uses it.


See e.g.
https://github.com/uwiger/parse_trans/blob/master/examples/test_exprecs_vsns.erl

-module(test_exprecs_vsns).

-export([f/0]).
-export_records([r]).

-compile({parse_transform, exprecs}).

-record(r, {a,b,c}).
-record(r__1_2, {a,b}).
-record(r__1_1, {a,b,c,d}).


f() ->
    io:fwrite("'#info-r'(fields) -> ~p~n", ['#info-r'(fields)]),
    io:fwrite("'#info-r__1_1'(fields)' -> ~p (not exported)~n",
	      ['#info-r__1_1'(fields)]),
    io:fwrite("'#info-r__1_2'(fields)' -> ~p (not exported)~n",
	      ['#info-r__1_2'(fields)]),
    io:fwrite("'#convert-'(\"1_1\", {r,1,2,3,4}) -> ~p~n",
	      ['#convert-'("1_1", {r,1,2,3,4})]),
    io:fwrite("'#convert-'(\"1_2\", {r,1,2}) -> ~p~n",
	      ['#convert-'("1_2", {r,1,2})]).

** Test run:

Eshell V5.9.2  (abort with ^G)
1> c(test_exprecs_vsns).
{ok,test_exprecs_vsns}
2> test_exprecs_vsns:f().
'#info-r'(fields) -> [a,b,c]
'#info-r__1_1'(fields)' -> [a,b,c,d] (not exported)
'#info-r__1_2'(fields)' -> [a,b] (not exported)
'#convert-'("1_1", {r,1,2,3,4}) -> {{r,1,2,3},[{d,4}]}
'#convert-'("1_2", {r,1,2}) -> {{r,1,2,undefined},[]}
ok

** Explanation:

The record ‘r’ is “exported”, meaning that exprecs generates accessor functions for it.

Old versions of the record definition are kept, but renamed as r__1_1, r__1_2, etc., respectively.

When old versions are present, exprecs will generate a ‘#convert-‘(OldVsn, Record) function that returns a record on the current format, together with a list of element that are no longer present.

Note that it doesn’t _convert_ elements; it simply maps them by name.

I probably should document this...

BR,
Ulf W

Ulf Wiger, Co-founder & Developer Advocate, Feuerlabs Inc.
http://feuerlabs.com



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20140203/88d98c84/attachment.html>


More information about the erlang-questions mailing list