question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

šŸš€ 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.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
rachel-barrettcommented, Sep 12, 2022

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.
1reaction
samhhcommented, Sep 11, 2022

NB there’s also sum-types.

Read more comments on GitHub >

github_iconTop Results From Across the Web

2909-destructuring-assignment - The Rust RFC Book
We propose to add special-casing for several classes of expressions on the left-hand side of an assignment, which act in accordance with destructuring...
Read more >
Destructuring assignment - JavaScript - MDN Web Docs
The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties fromĀ ...
Read more >
ADTs and Interfaces - Google Groups
I'd be very happy if some consideration was given to adding something like variant types to Go 2! I think it would help...
Read more >
union - Folktale
Constructs a tagged union data structure. ... Using the adt/union module: ... Folktale is in general mostly interested in correctness, and providing toolsĀ ......
Read more >
It's better to compose than inherit I know that this is just a ...
This is most clearly visible in OCaml's polymorphic variants [1, 2]. ... an exclusivity constraint, but it's a weird model and subtyping is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found