spartanz / schemaz

A purely-functional library for defining type-safe schemas for algebraic data types, providing free generators, SQL queries, JSON codecs, binary codecs, and migration from this schema definition
https://spartanz.github.io/schemaz
Apache License 2.0
164 stars 18 forks source link

Allow for recursive (self-referent) schemas #29

Closed vil1 closed 5 years ago

vil1 commented 5 years ago

We need a way to represent recursive types like:

sealed trait Tree
final case class Node(left: Tree, right: Tree) extends Tree
final case class Leaf(label: Int)              extends Tree

One idea (thx @julienrf) would be to have a dedicated member in the GADT for that:

final class SchemaReference[F[_], A](ref: => F[A]) extends Schema[F, A]

which would allow us to use lazy vals or def to build such recursive schema, but would require interpreters implementers (that is us, most of the time) to properly handle the laziness.

lazy val tree = union(
  "Node" -+>: record(
    "left" -*>: tree :*:
    "right" -*>: tree
  ) :+:
  "Leaf" -+>: record(
    "label" -*>: prim(IntSchema)
  )
)

Another, more "essential" way would be to find a way to encode the "y-combinator", or some kind of fix-point, at the schema level.

mijicd commented 5 years ago

Since it's considered "low-hanging fruit" as per @vil1, I'll try to take care of it.

vil1 commented 5 years ago

More precisely: "low hanging fruit hiding possible surprises" ;) Have fun!

GrafBlutwurst commented 5 years ago

54 should have closed this?