Records
From Erlang Community
(Difference between revisions)
| Revision as of 11:47, 15 July 2007 (edit) Bryan (Talk | contribs) (Added 'Concepts' Category tag.) ← Previous diff |
Current revision (10:48, 27 July 2007) (edit) (undo) Bryan (Talk | contribs) (Added how to get a value out of a record.) |
||
| Line 35: | Line 35: | ||
| <code> | <code> | ||
| elementTwoTimesEight( #recordname{ element2 = T } ) -> T * 8. | elementTwoTimesEight( #recordname{ element2 = T } ) -> T * 8. | ||
| + | </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> | </code> | ||
| Line 41: | Line 51: | ||
| <code> | <code> | ||
| - | + | 3> R2 = #recordname{}. % Uses the defaults. | |
| #recordname{element1 = "One",element2 = 2,element3 = three} | #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} | #recordname{element1 = "Two",element2 = 2,element3 = three} | ||
| </code> | </code> | ||
Current revision
Contents |
[edit] Defining Records
[edit] 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}).
|
[edit] 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 }).
|
[edit] Matching a Record
elementTwoTimesEight( #recordname{ element2 = T } ) -> T * 8.
|
[edit] 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
|
[edit] 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}
|
[edit] More Information
More information at http://erlang.org/doc/getting_started/records_macros.html#5.3.

Digg It
Del.icio.us
Reddit
Facebook
Stumble Upon
Technorati

