Generic lenses for F# record-types

F# record-types come in really handy when you need to model certain types of data-structures: Especially if you need to serialize them later on to send them over the wire or store them inside a database.

Check out Scott Wlaschins introduction to record-types in case you haven’t done so already:

They’re easy to use and let you build highly nested and relational data-structures in no-time. So fast in fact that I always forget how tedious it is to update and modify them, as you basically need to traverse up the entire tree to modify a single property at one end of the tree.

Knowing Haskells Lens library and how it solves this issue, I immediately went on the search for an F# library to help me out. Aether ( looks like a good implementation of Lenses but I wasn’t so happy about having to write the accessor functions for all the properties on my records.

Andrew Cherry goes a bit deeper into Lenses, Aether and the problems with updating record- types:

Since I wasn’t worrying about performance at all: Quotation-expressions and reflection seemed like a viable option and as it turns out this was way simpler to solve than I imagined.

Here is a nice post on what quotation-expressions are and how they can be useful by Tomas Petricek:

By wrapping the path to a property of a record type inside a quotation-expression you gain access to the entire tree-structure. That makes it easy to construct a copy of the tree and swap out the value of the given property.

I am quite happy with that solution as it doesn’t require any additional code and should work with every record-type; no matter how deeply nested they are.

Here are a couple of examples on how the above record (BusCompany) is modified by simply providing the property and the new value.

Not sure how useful this is going to be for others as it is certainly not the fastest way of doing this. It was really helpful though for sketching out data-types, models and their relationships…


As it turns Robert Jeppesen used the same technique and has a very similar implementation over here:

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.