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

Make incorrect records/unions impossible to construct (fixes #28) #35

Closed vil1 closed 5 years ago

vil1 commented 5 years ago

This is an easy, syntactic solution for #28.

We simply define a new ADT LabelledSum with its own :+: method accepting only LabelledSum arguments, and make the first argument for union be a LabelledSum instead of a more general FSchema (and respectively we define a LabelledProduct ADT for building records).

Making Union and Record (notice the renaming) be sealed abstract case classes make sure that they can only be instantiated through the union and record method (and therefore contain only labelled sum/products), without modifying the Schema ADT at all.

GrafBlutwurst commented 5 years ago

Does this mean it is no longer possible to have anonymous unions and records? what about schemas for (A, B) and A \/ B respectively?

vil1 commented 5 years ago

Yes, it is still possible to use :+: and :*: to build sums and product of unlabelled schemas (it is still used that way to represent options).