Instantiate types from recursive type grammar

2019-08-03 19:00发布

问题:

Given this recursive type grammar:

case class Fix[F[_]](out: F[Fix[F]])
type FieldValue = Seq[String] :+: String :+: Int :+: Long :+: CNil
type FieldLeaf[F] = FieldValue :+: SubField[F] :+: CNil
type SubField[F] = Seq[F]
type Field0[F] = (String, FieldLeaf[F])
type Field = Fix[Field0]

And instances of Seq[Field]

Is it feasible to instantiate concrete classes from the type grammar?

I.e:

def instantiate[T](fields:Seq[Field]):Option[T] = ....
case class Person(id:Long, name:String)
val fields:Seq[Field] = .... //seq of person fields
val person:Option[Person] = instantiate[Person](fields)

I imagine Shapeless could be used for this, like in the json-library Circe.


See also Describe recursive grammar with type aliases