Tutorial: LFE Records

2 Extracting Record Data

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

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

The following functions are available

   (make-person  ... )
   (match-person  ... )
   (is-person r)
   (emp-person  ... )
   (set-person r  ... )
   (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)

Taking our example from Creating a Record, we again define a person record, but this time we expose a function to print the information on a person.

(defmodule person
  (export (print-data 0)))

(defrecord person
  name
  address
  age)

(defun print-data ()
  (let ((ford (make-person name '"Ford Prefect"
                           address '"Betelgeuse Seven"
                           age 234)))
    (: io format '"~p~n" (list ford))
    (: io format '"~p~n" (list (person-name ford)))
    (: io format '"~p~n" (list (person-address ford)))
    (: io format '"~p~n" (list (person-age ford)))))