Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Variant module, for construction of and destructuring of general ADTS #1770

Open
rachel-barrett opened this issue Sep 10, 2022 · 6 comments · May be fixed by #1771
Open

Add a Variant module, for construction of and destructuring of general ADTS #1770

rachel-barrett opened this issue Sep 10, 2022 · 6 comments · May be fixed by #1771

Comments

@rachel-barrett
Copy link

🚀 Feature request

Desired Behavior

I think it would be good to have a module for helpers to construct Variant types (aka ADTs / Sum types / Discriminated unions) - the dual to Records. The advantage of such a module is that it reduces boiler plate and increases consistency when constructing algebraic data types.

The module would also have functions to apply functional pattern matching that doesn't rely on switch statements or the internals of the variant implementation.

Suggested Solution

I currently make use of my own library for this @rachel-barrett/variant-ts that I use alongside fp-ts, in both personal projects and at work. I think it could make sense for it to be a part of fp-ts. As far as I am aware there is not anything similar already in the fp-ts ecosystem?

I have also written a pull request against fp-ts to illustrate more directly how it could fit in to fp-ts, which I will link.

Who does this impact? Who is this for?

This would be beneficial for all typescript users wanting to use algebraic data types in a functional style.

@grossbart
Copy link
Collaborator

grossbart commented Sep 11, 2022

I definitely support the need for tooling like this. Whether or not it should be part of this library I can't say – for me, a standalone library works just as well and might allow to solve for different trade-offs.

There are some libraries that work well with fp-ts:

And now yours, which is nice and simple – thanks for that!

@rachel-barrett
Copy link
Author

Thanks for sharing those links! I wasn't aware of these libraries.

@samhh
Copy link
Contributor

samhh commented Sep 11, 2022

NB there's also sum-types.

@rachel-barrett rachel-barrett changed the title Add a Variant module, for construction of and pattern matching on general ADTS Add a Variant module, for construction of and destructuring of general ADTS Sep 11, 2022
@kalda341
Copy link

I really like the idea of having a module to help with this - having to open a block for a case statement often looks out of place and gets on my nerves.

@rachel-barrett
Copy link
Author

Some of my thoughts on the advantages and disadvantages of Variant (or equivalent) being part of fp-ts:

On the one hand I'm not sure that it fits in fp-ts since there's something slightly different about it as it doesn't follow the pattern of other modules. Also with variants not being native to typescript, it is harder to agree on a canonical way to implement them.

On the other hand, things that make me think it makes sense in fp-ts:

  • Variant is dual to Record which does have a module in fp-ts, so it is at the same "level"
  • Since some way of building variants is core to functional programming, it would be good to have a consistent "default" approach
  • If Variant was in fp-ts, the encoders and decoders could be in io-ts without needing a separate binding library.

@samhh
Copy link
Contributor

samhh commented Sep 13, 2022

Variant is dual to Record which does have a module in fp-ts, so it is at the same "level"

I wouldn't necessarily semantically agree. There are multiple modules that represent product types, for example also Tuple and Map. Duals therefore might be specific sums such as Option and Either. This is a different level of abstraction beneath the aforementioned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants