[ANN] fields-and-cases - Generate types for any language

I’d like to announce a new library: fields-and-cases. It generates type definitions for arbitrary programming languages. All based on Haskell types.

The README contains detailed example for generating types for Rust and TypeScript. The pattern can be easily applied to other languages. The library makes the heavy lifting for you - such as type level programming parts. All you have to do is to provide some instances and pattern match on simple data structures to define custom mappings to the target languages.

12 Likes

Since you mention JSON in the README, I suppose this is an alternative to generating OpenAPI definitions, and then using those to generate (de-)serialization for other languages.

But it isn’t clear to me which kinds of data types this would be advantageous to use, compared to OpenAPI.

In any case, I’d like to hear the motivation for this particular approach, if you have one.

2 Likes

Haskell types can be stronger than an Open API specs. Open API are structural types with a couple of refinement options (min/max integers, regex for strings…)
In Haskell you can describe more invariants with nominal types (smart constructor pattern)
If you generate via Open API you lose that.
With the fields-and-cases you have full control. Say you have a type EvenInt. Open API could not express this. But maybe inside of your client you also have a strong EvenInt type. So you could just reference this in the generated code if you arrange it to be imported.

That was my motivation behind the library. Generally I experienced the OpenAPI approach to be not flexible enough. You depend on the client generator which is often not configurable.

Let me know if you have more questions or concerns.

Looks interesting
while, is it possible to convert Haskell ADT type to Python ? I think Python doesn’t have such ADT type (especially with recursive reference within ADT ) native ? or fields-and-cases only apply to static typed language ?

@yellowbean The library is meant to generate static type definitions.

1 Like