Staying Out of Trouble

Possibly the easiest mistake you can make with a record is to attempt to get a value into a field and to miss. While this code may look plausible, it’s nonsense:

 (map->FictionalCharacter {:full-name ​"Elizabeth Bennet"
  :book ​"Pride & Prejudice"
  :written-by ​"Austen"​})

The trouble is that fields in FictionalCharacter are called name, appears-in, and author, not full-name, book, and written-by. The expression in the preceding code will give you a six-field record, with the three built-in fields set to nil, complemented by three additional fields—remember, records are also maps— set to the values we see in the code.

You can come to the same unfortunate end with botched uses of assoc:

 (assoc elizabeth :book ​"Pride ...

Get Getting Clojure now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.