1

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

Community
  • 1
  • 1
eirirlar
  • 814
  • 6
  • 24
  • What do you mean by instantiate concrete classes? What is `T` in your function? – OlivierBlanvillain Mar 03 '17 at 08:46
  • @OlivierBlanvillain see update. – eirirlar Mar 03 '17 at 09:57
  • Reply from the Shapeless gitter channel: You can do that with a mix of ToHList, Typeable and Generic. Yielding an Option[Person] of course. – eirirlar Mar 03 '17 at 09:57
  • Ended up recursively mapping everything to equivalent Circe Json constructs, because json is actually a pretty good representation of recursive data, and Circe has great support for decoding json to case classes and sealed traits. – eirirlar Mar 08 '17 at 11:58

0 Answers0