Erlang Central

Difference between revisions of "Records"

From ErlangCentral Wiki

(Added with and without defaults; Added how to modify a record.)
m (I messed up the layout a little bit with the last edit, so this fixes that.)
Line 16: Line 16:
  
 
=== With Defaults ===
 
=== With Defaults ===
 
== Creating a Record ==
 
  
 
Defining a record in code looks like this:
 
Defining a record in code looks like this:
Line 30: Line 28:
 
rd(recordname, {element1 = "One", element2 = 2, element3  = three }).
 
rd(recordname, {element1 = "One", element2 = 2, element3  = three }).
 
</code>
 
</code>
 +
  
 
== Matching a Record ==
 
== Matching a Record ==
Line 36: Line 35:
 
elementTwoTimesEight( #recordname{ element2 = T } ) -> T * 8.
 
elementTwoTimesEight( #recordname{ element2 = T } ) -> T * 8.
 
</code>
 
</code>
 +
  
 
== Modifying a Record ==
 
== Modifying a Record ==
Line 45: Line 45:
 
#recordname{element1 = "Two",element2 = 2,element3 = three}
 
#recordname{element1 = "Two",element2 = 2,element3 = three}
 
</code>
 
</code>
 +
  
 
== More Information ==
 
== More Information ==
  
 
More information at [http://erlang.org/doc/getting_started/records_macros.html#5.3 http://erlang.org/doc/getting_started/records_macros.html#5.3].
 
More information at [http://erlang.org/doc/getting_started/records_macros.html#5.3 http://erlang.org/doc/getting_started/records_macros.html#5.3].

Revision as of 11:45, 15 July 2007

Contents

Defining Records

Without Defaults

Defining a record in code looks like this:

-record( recordname, { element1, element2, element3 } ).

Defining one in the interpreter looks like this:

rd(recordname, {element1, element2, element3}).

With Defaults

Defining a record in code looks like this:

-record( recordname, { element1 = "One", element2 = 2, element3 = three } ).

Defining one in the interpreter (what I did to create these examples) looks like this:

rd(recordname, {element1 = "One", element2 = 2, element3  = three }).


Matching a Record

elementTwoTimesEight( #recordname{ element2 = T } ) -> T * 8.


Modifying a Record

2> R1 = #recordname{}.  % Uses the defaults. 
#recordname{element1 = "One",element2 = 2,element3 = three}
3> R2 = R1#recordname{ element1 = "Two" }.  % Only modify one part of the record.
#recordname{element1 = "Two",element2 = 2,element3 = three}


More Information

More information at http://erlang.org/doc/getting_started/records_macros.html#5.3.