I haven’t used records before in a project and when reading some code that used it, realize I really need to learn about records. It’s not complicated and actually kind of cool! Here’s my experimentation.
To start off, I re-read the first chapter in ClojureApplied where it talks about Modeling your Entities.
I took a familar problem, my recipe-api. This is a recipe api that uses liberator api reading recipes from a database. But, I can use the concept here.
When you create a record, you get two factory functions for free to create instances of it. The first, a positional function:
You must put values for all the fields in or you get
1 2 3
The other way you can create an instance of a record is using a map.
1 2 3 4
We didn’t have a value for
link in the map so it still let us create the instance and automatically assigned the value of nil. This might be a reason to use the
map-> version over the positional version, if you don’t know all the values at that time. They work like maps, you can add in missing values later with
Accessing the the values in a record is just like a map:
1 2 3 4
The chapter has a great section on why you would want to choose Records over Maps you should read! I won’t spoil the fun here, but there are some compelling reasons when to use records over a map.