fun icon indicating copy to clipboard operation
fun copied to clipboard

Move away from widen functions

Open baetheus opened this issue 3 years ago • 3 comments
trafficstars

Remove widen from the library and make the default exports for all ADTs widen where appropriate. The canonical algebraic structure exports will have tighter types.

baetheus avatar Mar 19 '22 15:03 baetheus

There is a reason for widening that is also appearing in the fetch_archive, which in production code is quite often coming up, because an operation will often yield a discriminated union of errors by adding different errors down the chain.

What other means of dealing with widening the left side could we come up with?

pixeleet avatar Mar 19 '22 19:03 pixeleet

Generally those issues come up in any function that can remap the left hand side of an ADT extending Either. My thought was to simply update the base exported functions with widening in their types. For example,

export function chain<A, I, J>(
  fati: (a: A) => Either<J, I>,
): ((ta: Either<B, A>) => Either<B | J, I>) {
  return (ta) => isLeft(ta) ? ta : fati(ta.right);
}

This would make the working exports from each ADT have type widening built in, but the canonical "Type Class" exports would have type narrowing, which is what you want if you're taking a general Monad and extending it. Thoughts?

baetheus avatar Mar 20 '22 01:03 baetheus

Auto-widening the Left side in chaining is desirable and saves us from having to implement a specific chainW or bindW where fp-ts went. I would reason here that it's logical that a different operation can have a different Left side.

pixeleet avatar Mar 20 '22 14:03 pixeleet