Erlang Central

Difference between revisions of "Records"

From ErlangCentral Wiki

(Added a link to Erlang.org.)
(Added how to get a value out of a record.)
(3 intermediate revisions by one user not shown)
Line 1:Line 1:
 +
[[Category:Concepts]]
 
== Defining Records ==
 
== Defining Records ==
 +
 +
=== Without Defaults ===
 +
 +
Defining a record in code looks like this:
  
 
<code>
 
<code>
Line 5:Line 10:
 
</code>
 
</code>
  
== Creating a Record ==
+
Defining one in the interpreter looks like this:
  
 
<code>
 
<code>
R = #recordname{ element1 = "One", element2 = 2, element3 = three }.
+
rd(recordname, {element1, element2, element3}).
 
</code>
 
</code>
 +
 +
=== With Defaults ===
 +
 +
Defining a record in code looks like this:
 +
 +
<code>
 +
-record( recordname, { element1 = "One", element2 = 2, element3 = three } ).
 +
</code>
 +
 +
Defining one in the interpreter (what I did to create these examples) looks like this:
 +
 +
<code>
 +
rd(recordname, {element1 = "One", element2 = 2, element3  = three }).
 +
</code>
 +
  
 
== Matching a Record ==
 
== Matching a Record ==
Line 16:Line 36:
 
elementTwoTimesEight( #recordname{ element2 = T } ) -> T * 8.
 
elementTwoTimesEight( #recordname{ element2 = T } ) -> T * 8.
 
</code>
 
</code>
 +
 +
== Getting the value of a record element ==
 +
 +
<code>
 +
1> R1 = #recordname{}.  % Uses the defaults.
 +
#recordname{element1 = "One",element2 = 2,element3 = three}
 +
2> io:format("~p~n", [R1#recordname.element1]). % prints "One"
 +
"One"
 +
ok
 +
</code>
 +
 +
 +
== Modifying a Record ==
 +
 +
<code>
 +
3> R2 = #recordname{}.  % Uses the defaults.
 +
#recordname{element1 = "One",element2 = 2,element3 = three}
 +
4> R3 = R2#recordname{ element1 = "Two" }.  % Only modify one part of the record.
 +
#recordname{element1 = "Two",element2 = 2,element3 = three}
 +
</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 10:48, 27 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.

Getting the value of a record element

1> R1 = #recordname{}.  % Uses the defaults.
#recordname{element1 = "One",element2 = 2,element3 = three}
2> io:format("~p~n", [R1#recordname.element1]). % prints "One"
"One"
ok


Modifying a Record

3> R2 = #recordname{}.  % Uses the defaults. 
#recordname{element1 = "One",element2 = 2,element3 = three}
4> R3 = R2#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.