shapeless-contrib aims to provide smooth interoperability between Shapeless, and Spire. The most prominent feature is automatic derivation of type class instances for case classes, but there is more. If you've got a cool idea for more stuff, please open an issue.

Examples

Derive type classes

Consider a simple case class with an addition operation:

If we want to use that in a generic setting, e.g. in an algorithm which requires a Monoid, we can define an instance for spire.algebra.Monoid like so:

implicitobjectVector3MonoidextendsMonoid[Vector3] {
defid=Vector3(0, 0, 0)
defop(x: Vector3, y: Vector3) = x + y
}

This will work nicely for that particular case. However, it requires repetition: addition on Vector3 is just pointwise addition of its elements, and the null vector consists of three zeroes. We do not want to repeat that sort of code for all our case classes, and want to derive that automatically. Luckily, this library provides exactly that:

importspire.implicits._importshapeless.contrib.spire._// Define the `Vector3` case class without any operationscaseclassVector3(x: Int, y: Int, z: Int)
// That's it! `Vector3` is an `AdditiveMonoid` now.Vector3(1, 2, 3) +Vector3(-1, 3, 0)