Instantiate types from recursive type grammar

我怕爱的太早我们不能终老 提交于 2019-12-24 08:46:45

问题


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

来源:https://stackoverflow.com/questions/42573747/instantiate-types-from-recursive-type-grammar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!