I want to implement a type based id generator that I can call like this:
val nextPersonId = idGen.id[Person]
I'd like IdGen to be a trait (if possible):
trait IdGen[L <: HList] {
def id[T] = {
if (T is an element in the L type) return 0L + number of previous calls for this T
else throw new RuntimeException("no such T in L")
}
}
class MyDao extends IdGen[Person :: Booking :: Charges :: HNil] {
//something needed here?
}
How does one go about to implement this with Shapeless?
I've tried using scala reflection typeTag and iterate over the toString result, but it's ugly.