Reputation: 241
I am learning about shapeless type class derivation. One common exercises is to generate a random instance of a given case class. Simply put, the idea is to have something like the following:
trait Random[C] {
def gen: C
}
// primitives' instances
implicit val rs: Random[String] = ...
implicit val rd: Random[Boolean] = ...
implicit val rb: Random[Boolean] = ...
// HList and HNil instances
implicit val rhlist: Random[H :: T] = ...
implicit val rhnil: Random[HNil]
With an implicit shapeless.Generic[C]
, we can have then a random instance of C
. This works well, however I want my generator to respect some rules. For example, Random[Double]
should also generate not only a random double, but one that respects some bounds. The bound can be provided using an annotation on that field (case class Foo(@GreaterThan(0) x: Double)
) or using refined types (case class Foo(x: Double Refined Positive)
).
I am aware that the refined types approach might be totally different than annotations. I would like to explore both approaches, or the more idiomatic one. Do you have any hints ? Thanks
Upvotes: 0
Views: 113