Defining an Ordering for java.time.Period

I’ve written a library called intime, which provides exhaustive integration between the classes in the java.time library and some common Scala libraries. The most interesting problem I encountered was defining an Ordering (or an Order for Cats) for the java.time.Period class. Because months and years cannot simply be expressed as a number of days, This post will discuss those issues.

InvariantK

In my previous post, I discussed using Invariant to add behaviour to value classes. Unfortunately, Invariant is not powerful enough to provide instances for higher-kinded type classes like Functor or Traverse. In this post, I’ll introduce InvariantK, a type class I’ve written to solve this limitation.

Type class instances for value classes with Invariant

Previously I discussed about the advantages of wrapping common types like Int or String in a value class. This allows us to encode more semantic meaning into our types, and means we can use the compiler to check for a number of bugs. In this post I’ll discuss how we can use the Invariant type class to selectively surface functionality from the underlying type for our value class.

Value classes

One of the most valuable techniques I’ve learned from strongly-typed functional programming is value classes. By wrapping common types in specialised case classes, we can improve semantic clarity and leverage compile-time checks to avoid bugs.