Blog
Copying fields using Shapeless - Part 3
In my second post I explained how shapeless can add, remove and align fields from case classes to match another type. The code works, but it’s pretty clunky. In this post I will clean it up a bit to make it less verbose To follow along it’s best to check out the accompanying source code from my Github repo and checkout the 9-cleanup tag Automatically dropping unused fields In the current implementation I need to manually drop the isDiesel flag from the Vehicle as it’s not needed by the Reservation case class.
Copying fields using Shapeless - Part 2
In my first post I explained how shapeless can Transform a case class to an Hlist Concatenate hlists Transform an hlist back to another case class There are some serious limitations to our previous implementation: We need to ensure the case class fields are ordered correctly. If the order of the fields change the code will either not compile (with LabelledGeneric); or worse it may compile and run but return the wrong date.
Copying fields using Shapeless - Part 1
Sometimes we need to copy fields from one case class to another. Often the two case classes are quite similar, but we still need to copy each field manually. Shapeless can eliminate this boilerplate for us. First let’s think why we may want to do this. We often face the scenario of collecting information in multiple steps, validating it along the way and performing the final operation when we have everything we need.
Shapeless Coproducts
We’re all used to pattern matching across traits (typically sealed) but Shapeless gives us a more flexible approach. Shapeless Coproducts are not necessarily always the best option though. To find out why and when you should use this new pattern read on … What’s wrong with sealed traits? You are no doubt familiar with sealed traits and pattern matching on them: sealed trait Error case class BadName(name: String) extends Error case class BadAge(age: Int) extends Error .