Shapeless: restricting case class types

2019-07-11 01:59发布

(NOTE: split from Shapeless: Trying to restrict HList elements by their type and Shapeless: own HList constraint using Coproduct )

Question 3 - restrict case classes by parameter types

A very nice additional gain would be, if I could use HList constraints to constrain case class only to be built from AnyVals, Strings, and a specific MyBaseTrait, that recursively fulfill the same constraint. The constraint being defined on the base trait and not to have to touch any derived case class would be great. Something like:

trait MyBaseTrait
type MyCP = Boolean :+: Long :+: ... :+: String :+: MyBaseTrait
case class ValidSimple(b: Boolean, s: String) extends MyBaseTrait
case class ValidNested(s: String, vs: ValidSimple) extends MyBaseTrait
case class NotOfBaseTrait(l: Long)

case class InvalidNested(s: String, vs: NotOfBaseTrait) extends MyBaseTrait // => compile error

This would allow to fail a lot earlier. Is this possible? via Generic#Repr or DeepHList?

0条回答
登录 后发表回答