05 / 10
Records
A type with named fields, built by name, read with a dot, checked when it is made.
A map can hold anything under any key. A record is stricter: a type with a name and a fixed list of fields. deftype defines one.
(deftype Ship [name seats]) (def seeker (Ship name: "Seeker" seats: 3)) (defn main [args] ;; A record prints the way it is built. (print seeker) ;; A dot reads a field. (print seeker.name seeker.seats) ;; The labels may come in any order, and = compares the fields. (print (= seeker (Ship seats: 3 name: "Seeker"))) ;; A record is a value: assoc gives a new one. (print (assoc seeker :seats 5)) (print seeker))
Try it: Build a ship with a field it does not have: (Ship name: "Ark" seats: 500 sails: 2).
A record is built by naming its fields, name: and seats:, in any order. It prints the way it was built. And it is a value: assoc gives a new ship and leaves the old one alone.
A record knows its fields
Reading a field a record does not have is an error, and the report lists the fields it does have. So is leaving one out when you build it.
(deftype Ship [name seats]) (def seeker (Ship name: "Seeker" seats: 3)) (defn main [args] (print seeker.name) (print seeker.saets))
Try it: Fix the field's name, and the program finishes.
(deftype Ship [name seats]) (defn main [args] (print (Ship name: "Seeker")))
Try it: Add seats: 3.
Compare with a map, where get answers nil for a key that is not there. A map is open: anything may be in it. A record is closed, so a wrong name can only be a mistake, and it is caught where it is made.
Defaults and rules
A field written in brackets with a value has a default. And a type may carry a rule: where: is followed by a test that every value of the type must pass, checked each time one is built.
;; A field in brackets has a default. where: is checked at every construction. (deftype Ship [name (seats 2) (crew #[])] where: (<= (count crew) seats)) (defn board [ship person] (Ship name: ship.name seats: ship.seats crew: (conj ship.crew person))) (defn main [args] (let [seeker (Ship name: "Seeker")] (print seeker) (print (board seeker "Alvin")) (print (board (board seeker "Alvin") "Hilvar"))))
Try it: Board a third person. The ship has two seats, and the rule says no.
This is what the report looks like. It shows the rule that failed and the value that failed it.
(deftype Ship [name (seats 2) (crew #[])] where: (<= (count crew) seats)) (defn main [args] (print (Ship name: "Seeker" crew: #["Alvin" "Hilvar" "Alystra"])))
Try it: Give the ship seats: 3, and it takes all three.
Since a ship can only be made by building one, and building one checks the rule, there is no ship in the program with more people than seats. Code that receives a Ship does not have to check again.
Taking a record apart
A record is taken apart the way it is built. Where a name could stand, write the record's shape, and the fields you mention become names.
(deftype Ship [name seats]) (deftype Trip [ship from to]) (def trip (Trip ship: (Ship name: "Seeker" seats: 3) from: "Diaspar" to: "Lys")) ;; A parameter may be a pattern: this function takes a Trip apart. (defn describe [(Trip ship: from: to:)] f"{ship.name} goes from {from} to {to}") (defn main [args] (print (describe trip)) ;; Dots chain, and a pattern in let reaches inside. (print trip.ship.name) (let [(Trip ship: (Ship seats:)) trip] (print "seats:" seats)))
Try it: In describe, also take the ship's name out in the parameter: (Trip ship: (Ship name:) from: to:), and use name in the string.
These shapes are called patterns, and the same ones work in let, in parameters and in match. They have a page of their own. First, the other kind of type: one that is one of several shapes.