Playing around with Shapeless to require certain Fields w/o any boilerplate.
The goal is to provide a boilerplate-free infrastructure for extracting certain fields out of a record type.
Say, For example, a database service might require an ID for every entity. The typical OO solutions would provide a base class with an abstract ID field or rely on annotations and reflections to specify the ID field. Such solutions often bind the entity/model class directly to the database implementation or, at best, to something like JPA.
A solution where JPA can be called "best" is hardly a good thing.
HasId typeclass in combination with shapeless generic programming is an approach to
extract the ID value from any arbitrary case class (record type, really) without any
boilerplate code required at all.
It requires a certain field with a specific type, but this is no different than
requiring an interface/trait with this particular abstract method to be implemented.
The users' model definition needn't to depend on third-party libraries (not even shapeless), so it can be truly dependency free.
HasId typeclass can be provided either by an independent library or the database library
and the latter of which can provide an API like
def save[A: HasId](userEntity: A) and can thus
support any user defined type.
Invalid types will be sorted out at compile-time and no runtime reflection is involved.
The user may choose to couple their models to
HasId by caching the implicits instances in order to
reduce compile time, but this is not necessary.