1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- This document was generated using DocBuilder 3.3.3 -->
<HTML>
<HEAD>
<TITLE>Records</TITLE>
<SCRIPT type="text/javascript" src="../../doc/erlresolvelinks.js">
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#FF00FF"
ALINK="#FF0000">
<CENTER>
<A HREF="http://www.erlang.se"><IMG BORDER=0 ALT="[Ericsson AB]" SRC="min_head.gif"></A>
</CENTER>
<A NAME="8"><!-- Empty --></A>
<H2>8 Records</H2>
<P>A record is a data structure for storing a fixed number of
elements. It has named fields and is similar to a struct in C.
Record expressions are translated to tuple expressions during
compilation. Therefore, record expressions are not understood by
the shell unless special actions are taken. See <CODE>shell(3)</CODE>
for details.
<P>More record examples can be found in <STRONG>Programming Examples</STRONG>.
<A NAME="8.1"><!-- Empty --></A>
<H3>8.1 Defining Records</H3>
<P>A record definition consists of the name of the record,
followed by the field names of the record. Record and field names
must be atoms. Each field can be given an optional default value.
If no default value is supplied, <CODE>undefined</CODE> will be used.
<PRE>
-record(Name, {Field1 [= Value1],
...
FieldN [= ValueN]}).
</PRE>
<P>A record definition can be placed anywhere among the attributes
and function declarations of a module, but the definition must
come before any usage of the record.
<P>If a record is used in several modules, it is recommended that
the record definition is placed in an include file.<A NAME="8.2"><!-- Empty --></A>
<H3>8.2 Creating Records</H3>
<P>The following expression creates a new <CODE>Name</CODE> record where
the value of each field <CODE>FieldI</CODE> is the value of evaluating
the corresponding expression <CODE>ExprI</CODE>:
<PRE>
#Name{Field1=Expr1,...,FieldK=ExprK}
</PRE>
<P>The fields may be in any order, not necessarily the same order as
in the record definition, and fields can be omitted. Omitted
fields will get their respective default value instead.
<P>If several fields should be assigned the same value,
the following construction can be used:
<PRE>
#Name{Field1=Expr1,...,FieldK=ExprK, _=ExprL}
</PRE>
<P>Omitted fields will then get the value of evaluating <CODE>ExprL</CODE>
instead of their default values. This feature was added in
Erlang 5.1/OTP R8 and is primarily intended to be used to create
patterns for ETS and Mnesia match functions. Example:
<PRE>
-record(person, {name, phone, address}).
...
lookup(Name, Tab) ->
ets:match_object(Tab, #person{name=Name, _='_'}).
</PRE>
<A NAME="8.3"><!-- Empty --></A>
<H3>8.3 Accessing Record Fields</H3>
<PRE>
Expr#Name.Field
</PRE>
<P>Returns the value of the specified field. <CODE>Expr</CODE> should
evaluate to a <CODE>Name</CODE> record.
<P>The following expression returns the position of the specified
field in the tuple representation of the record:
<PRE>
#Name.Field
</PRE>
<P>Example:
<PRE>
-record(person, {name, phone, address}).
...
lookup(Name, List) ->
lists:keysearch(Name, #person.name, List).
</PRE>
<A NAME="8.4"><!-- Empty --></A>
<H3>8.4 Updating Records</H3>
<PRE>
Expr#Name{Field1=Expr1,...,FieldK=ExprK}
</PRE>
<P><CODE>Expr</CODE> should evaluate to a <CODE>Name</CODE> record. Returns a
copy of this record, with the value of each specified field
<CODE>FieldI</CODE> changed to the value of evaluating the corresponding
expression <CODE>ExprI</CODE>. All other fields retain their old
valules.
<P><A NAME="8.5"><!-- Empty --></A>
<H3>8.5 Records in Guards</H3>
<P>Since record expressions are expanded to tuple expressions,
creating records and accessing record fields are allowed in
guards. However all subexpressions, for example for field
initiations, must of course be valid guard expressions as well.
Examples:
<PRE>
handle(Msg, State) when Msg==#msg{to=void, no=3} ->
...
handle(Msg, State) when State#state.running==true ->
...
</PRE>
<P>There is also a type test BIF <CODE>is_record(Term, RecordTag)</CODE>.
Example:
<PRE>
is_person(P) when is_record(P, person) ->
true;
is_person(_P) ->
false.
</PRE>
<A NAME="8.6"><!-- Empty --></A>
<H3>8.6 Records in Patterns</H3>
<P>A pattern that will match a certain record is created the same
way as a record is created:
<PRE>
#Name{Field1=Expr1,...,FieldK=ExprK}
</PRE>
<P>In this case, one or more of <CODE>Expr1</CODE>...<CODE>ExprK</CODE> may be
unbound variables.<A NAME="8.7"><!-- Empty --></A>
<H3>8.7 Internal Representation of Records</H3>
<P>Record expressions are translated to tuple expressions during
compilation. A record defined as
<PRE>
-record(Name, {Field1,...,FieldN}).
</PRE>
<P>is internally represented by the tuple
<PRE>
{Name,Value1,...,ValueN}
</PRE>
<P>where each <CODE>ValueI</CODE> is the default value for <CODE>FieldI</CODE>.
<P>To each module using records, a pseudo function is added
during compilation to obtain information about records:
<PRE>
record_info(fields, Record) -> [Field]
record_info(size, Record) -> Size
</PRE>
<P><CODE>Size</CODE> is the size of the tuple representation, that is
one more than the number of fields.<CENTER>
<HR>
<SMALL>
Copyright © 1991-2006
<A HREF="http://www.erlang.se">Ericsson AB</A><BR>
</SMALL>
</CENTER>
</BODY>
</HTML>
|