pfgray / ts-adt

Generate Algebraic Data Types and pattern matchers
MIT License
314 stars 13 forks source link

Add a default `otherwise` to PI matcher #33

Closed IgnusG closed 2 years ago

IgnusG commented 3 years ago

If we only want to modify a specific branch leaving the other intact this reduces boilerplate. This can come up very often for example with an Optional ADT if we chain modifying Some values leaving None intact.

We can write:

const result = matchPI(a)({
  Ok: ({ value }) => ({ value: `Cool: ${value}` }),
});

instead of

const result = matchPI(a)({
  Ok: ({ value }) => ({ value: `Cool: ${value}` }),
}, (v) => v);

I'm sorry for the newline change at the end. I've used GitHub for editing the file and it did that automatically. Please let me know if you want me to fix that.

I have not run any test on this change yet. It's possible there are other parts of code that need changing? Let me know if I missed something.

Finally thank you for this library! I find it very useful in creating simple Optional/Result etc. data types ❤️

IgnusG commented 2 years ago

Closing for cleanup