[erlang-questions] Add a field to a record

Marc Worrell <>
Fri Sep 18 10:20:39 CEST 2015


Hi Lloyd,

That is a very common situation.

We have two tactics to handle that:

 1) Just let it crash and then restart with the new definition
 2) If (1) is not acceptable, use module/function that can “upgrade” your record.

Example of #2:

	upgrade(#anything{} = Rec) -> Rec;
	upgrade({anything, Field1, Field2}) -> #anything{field1=Field1, field2=Field2}.

And then, if you receive a record from old(er) code/nodes or a backing store, first “upgrade” your record before you do anything with it.

	do_something(Rec0) ->
		Rec = upgrade(Rec0),
		… your code with Rec …

- Marc


> On 18 sep. 2015, at 00:16,  wrote:
> 
> Hello,
> 
> I understand that it would be better to use maps if you anticipate adding fields to a record.
> 
> But suppose you didn't anticipate and you've been merrily adding data to your db when you discover--- oops! I need another field.
> 
> e.g. You have: 
>       -record anything, {
>            field1,
>            field2
>          }
> 
>      and you discover that you really need:
>       -record anything, {
>            field1,
>            new_field,
>            field2
>          }
> 
> Is there a cunning trick or technique to make the change without losing data?
> 
> Thanks,
> 
> LRP
> 
> 
> *********************************************
> My books:
> 
> THE GOSPEL OF ASHES
> http://thegospelofashes.com
> 
> Strength is not enough. Do they have the courage 
> and the cunning? Can they survive long enough to 
> save the lives of millions?  
> 
> FREEIN' PANCHO
> http://freeinpancho.com
> 
> A community of misfits help a troubled boy find his way 
> 
> AYA TAKEO
> http://ayatakeo.com
> 
> Star-crossed love, war and power in an alternative 
> universe
> 
> Available through Amazon or by request from your 
> favorite bookstore
> 
> 
> **********************************************
> 
> 
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions



More information about the erlang-questions mailing list