Records

Records are tuples with the record name as first element and the rest of the fields in order exactly like "normal" Erlang records. As with Erlang records the default default value is 'undefined.

(defrecord name
  field
  (field default-value)
  ... )

The record defined above will create access functions/macros for creation and access fields. The make, match and set forms takes optional argument pairs field-name value to get non-default values. E.g. for the record:

(defrecord person
  (name '"")
  (address '"")
  age)

We have:

  • (make-person {{field value}} ... )
  • (match-person {{field value}} ... )
  • (is-person r)
  • (fields-person)
  • (emp-person {{field value}} ... )
  • (set-person r {{field value}} ... )
  • (person-name r)
  • (person-name)
  • (set-person-name r name)
  • (person-age r)
  • (person-age)
  • (set-person-age r age)
  • (person-address r)
  • (set-person-address r address)

Examples

(make-person name "Robert" age 54)

Will create a new person record with the name field set to "Robert", the age field set to 54 and the address field set to the default "".

(match-person name name age 55)

Will match a person with age 55 and bind the variable name to the name field of the record. Can use any variable name here.

(is-person john)

Test if john is a person record.

(emp-person age '$1)

Create an Ets Match Pattern for record person where the age field is set to $1 and all other fields are set to _.

(person-address john)

Return the address field of the person record john.

(person-address)

Return the index of the address field of a person record.

(set-person-address john '"back street")

Sets the address field of the person record john to "back street".

(set-person john age 35 address '"front street")

In the person record john set the age field to 35 and the address field to "front street".

(fields-person)

Returns a list of fields for the record. This is useful for when using LFE with Mnesia, as the record field names don't have to be provided manually in the create_table call.