Even though in TypeScript the Funfix library is using abstract class to
express type classes, when implementing this type class it is recommended
that you implement it as a mixin using "implements", instead of extending
it directly with "extends". See
TypeScript: Mixins
for details and note that we already have applyMixins defined.
Implementation example:
import {
HK,
ApplicativeError,
registerTypeClassInstance,
applyMixins,
Try
} from"funfix"// Type alias defined for readability.// HK is our encoding for higher-kinded types.type BoxK<T> = HK<Box<any>, T>
class Box<T> implements HK<Box<any>, T> {
constructor(public value: Try<T>) {}
// Implements HK<Box<any>, A>, not really needed, but useful in order// to avoid type casts. Note they can and should be undefined:
readonly _funKindF: Box<any>
readonly _funKindA: T
}
class BoxApplicativeError implements ApplicativeError<Box<any>, any> {
pure<A>(a: A): Box<A> { returnnew Box(Try.success(a)) }
ap<A, B>(fa: BoxK<A>, ff: BoxK<(a: A) => B>): Box<B> {
constta = (fa as Box<A>).valueconsttf = (ff as Box<(a: A) => B>).valuereturnnewBox(Try.map2(ta, tf, (a, f) => f(a)))
}
raise<A>(e: any): HK<Box<any>, A> {
returnnewBox(Try.failure(e))
}
recoverWith<A>(fa: BoxK<A>, f: (e: any) => BoxK<A>): HK<Box<any>, A> {
returnnewBox((fa as Box<A>).value.recoverWith(e => (f(e) as Box<A>).value))
}
// Mixed-in, asthesehavedefaultimplementationsmap: <A, B>(fa: BoxK<A>, f: (a: A) => B) => Box<B>
map2: <A, B, Z>(fa: BoxK<A>, fb: BoxK<B>, f: (a: A, b: B) => Z) => Box<Z>
product: <A, B> (fa: BoxK<A>, fb: BoxK<B>) => Box<[A, B]>
unit: () => Box<void>
recover: <A>(fa: HK<Box<any>, A>, f: (e: any) => A) => HK<Box<any>, A>
attempt: <A>(fa: HK<Box<any>, A>) => HK<Box<any>, Either<any, A>>
}
// Call needed in order to implement `map`, `map2`, `product`, etc.// using the default implementations defined by `ApplicativeError`,// because we are using `implements` instead of `extends` above and// because in this sample we want the default implementations,// but note that you can always provide your own
applyMixins(BoxApplicativeError, [ApplicativeError])
// Registering global ApplicativeError instance for Box, needed in order// for the `functorOf(Box)`, `applyOf(Box)`, `applicativeOf(Box)`// and `applicativeErrorOf(Box)` calls to work
registerTypeClassInstance(ApplicativeError)(Box, new BoxApplicativeError())
We are using implements in order to support multiple inheritance and to
avoid inheriting any static members. In the Flow definitions (e.g.
.js.flow files) for Funfix these type classes are defined with
"interface", as they are meant to be interfaces that sometimes have
default implementations and not classes.
Credits
This type class is inspired by the equivalent in Haskell's
standard library and the implementation is inspired by the
Typelevel Cats project.
The
ApplicativeErrortype class is a Applicative that also allows you to raise and or handle an error value.This type class allows one to abstract over error-handling applicative types.
MUST follow the law defined in ApplicativeErrorLaws.
Implementation notes
Even though in TypeScript the Funfix library is using
abstract classto express type classes, when implementing this type class it is recommended that you implement it as a mixin using "implements", instead of extending it directly with "extends". See TypeScript: Mixins for details and note that we already haveapplyMixinsdefined.Implementation example:
import { HK, ApplicativeError, registerTypeClassInstance, applyMixins, Try } from "funfix" // Type alias defined for readability. // HK is our encoding for higher-kinded types. type BoxK<T> = HK<Box<any>, T> class Box<T> implements HK<Box<any>, T> { constructor(public value: Try<T>) {} // Implements HK<Box<any>, A>, not really needed, but useful in order // to avoid type casts. Note they can and should be undefined: readonly _funKindF: Box<any> readonly _funKindA: T } class BoxApplicativeError implements ApplicativeError<Box<any>, any> { pure<A>(a: A): Box<A> { return new Box(Try.success(a)) } ap<A, B>(fa: BoxK<A>, ff: BoxK<(a: A) => B>): Box<B> { const ta = (fa as Box<A>).value const tf = (ff as Box<(a: A) => B>).value return new Box(Try.map2(ta, tf, (a, f) => f(a))) } raise<A>(e: any): HK<Box<any>, A> { return new Box(Try.failure(e)) } recoverWith<A>(fa: BoxK<A>, f: (e: any) => BoxK<A>): HK<Box<any>, A> { return new Box((fa as Box<A>).value.recoverWith(e => (f(e) as Box<A>).value)) } // Mixed-in, as these have default implementations map: <A, B>(fa: BoxK<A>, f: (a: A) => B) => Box<B> map2: <A, B, Z>(fa: BoxK<A>, fb: BoxK<B>, f: (a: A, b: B) => Z) => Box<Z> product: <A, B> (fa: BoxK<A>, fb: BoxK<B>) => Box<[A, B]> unit: () => Box<void> recover: <A>(fa: HK<Box<any>, A>, f: (e: any) => A) => HK<Box<any>, A> attempt: <A>(fa: HK<Box<any>, A>) => HK<Box<any>, Either<any, A>> } // Call needed in order to implement `map`, `map2`, `product`, etc. // using the default implementations defined by `ApplicativeError`, // because we are using `implements` instead of `extends` above and // because in this sample we want the default implementations, // but note that you can always provide your own applyMixins(BoxApplicativeError, [ApplicativeError]) // Registering global ApplicativeError instance for Box, needed in order // for the `functorOf(Box)`, `applyOf(Box)`, `applicativeOf(Box)` // and `applicativeErrorOf(Box)` calls to work registerTypeClassInstance(ApplicativeError)(Box, new BoxApplicativeError())We are using
implementsin order to support multiple inheritance and to avoid inheriting anystaticmembers. In the Flow definitions (e.g..js.flowfiles) for Funfix these type classes are defined with "interface", as they are meant to be interfaces that sometimes have default implementations and not classes.Credits
This type class is inspired by the equivalent in Haskell's standard library and the implementation is inspired by the Typelevel Cats project.