pfgray / ts-adt

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

Make Partial Matcher Signature work better with callbacks #13

Closed ryanleecode closed 3 years ago

ryanleecode commented 3 years ago
matchPI(promise)({ nothing: () => 0 }, (rest) => {
  // rest inferred as {_type: 'left', value: number } | { 'right', value: number }
  return rest.value;
});

This signature should be reversed imo. so you can specific the matchers and have it still work in a callback.

i.e.

myCallback(matchPI({ nothing: () => 0 }, (rest) => {
  // rest inferred as {_type: 'left', value: number } | { 'right', value: number }
  return rest.value;
}))

which is just 

myCallback((promise) => matchPI({ nothing: () => 0 }, (rest) => {
  // rest inferred as {_type: 'left', value: number } | { 'right', value: number }
  return rest.value;
})(promise))
pfgray commented 3 years ago

@ryanleecode , Does matchP meet your needs (this was the intention)?

myCallback(matchP({ nothing: () => 0 }, (rest) => {
  // rest inferred as {_type: 'left', value: number } | { 'right', value: number }
  return rest.value;
}))

Which also makes it compose well in a pipe:

pipe(
  value,
  matchP({...}, rest => {...})
)
ryanleecode commented 3 years ago

Oh i totally missed that. I stands for inverted, I see!

pfgray commented 3 years ago

Yup, in all fairness, the documentation could explain the differences between them!