Options
All
  • Public
  • Public/Protected
  • All
Menu

The MonadError type 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 MonadErrorLaws.

Implementation notes

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,
  MonadError,
  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 BoxMonadError implements MonadError<Box<any>, any> {
  pure<A>(a: A): Box<A> { return new Box(Try.success(a)) }

  flatMap<A, B>(fa: BoxK<A>, f: (a: A) => BoxK<B>): Box<B> {
    throw new NotImplementedError("Provide implementation")
  }

  tailRecM<A, B>(a: A, f: (a: A) => BoxK<Either<A, B>>): Box<B> {
    throw new NotImplementedError("Provide implementation")
  }

  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
  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>
  ap: <A, B>(fa: BoxK<A>, ff: BoxK<(a: A) => B>) => Box<B>
  product: <A, B> (fa: BoxK<A>, fb: BoxK<B>) => Box<[A, B]>
  unit: () => Box<void>
  followedBy: <A, B>(fa: BoxK<A>, fb: BoxK<B>) => Box<B>
  followedByL: <A, B>(fa: BoxK<A>, fb: () => BoxK<B>) => Box<B>
  forEffect: <A, B>(fa: BoxK<A>, fb: BoxK<B>) => Box<A>
  forEffectL: <A, B>(fa: BoxK<A>, fb: () => BoxK<B>) => Box<A>
  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 `MonadError`,
// 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(BoxMonadError, [MonadError])

// Registering global MonadError instance for Box, needed in order
// for the `functorOf(Box)`, `applyOf(Box)`, `applicativeOf(Box)`
// and `monadErrorOf(Box)` calls to work
registerTypeClassInstance(MonadError)(Box, new BoxMonadError())

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.

Type parameters

  • F

  • E

Hierarchy

  • MonadError

Implements

Implemented by

Index

Properties

ap

ap: function

Mixed-in from Monad.ap.

Type declaration

    • <A, B>(fa: HK<F, A>, ff: HK<F, function>): HK<F, B>
    • Type parameters

      • A

      • B

      Parameters

      • fa: HK<F, A>
      • ff: HK<F, function>

      Returns HK<F, B>

attempt

attempt: function

Mixed-in from ApplicativeError.attempt.

Type declaration

    • <A>(fa: HK<F, A>): HK<F, Either<E, A>>
    • Type parameters

      • A

      Parameters

      • fa: HK<F, A>

      Returns HK<F, Either<E, A>>

followedBy

followedBy: function

Mixed-in from FlatMap.followedBy.

Type declaration

    • <A, B>(fa: HK<F, A>, fb: HK<F, B>): HK<F, B>
    • Type parameters

      • A

      • B

      Parameters

      • fa: HK<F, A>
      • fb: HK<F, B>

      Returns HK<F, B>

followedByL

followedByL: function

Mixed-in from FlatMap.followedByL.

Type declaration

    • <A, B>(fa: HK<F, A>, fb: function): HK<F, B>
    • Type parameters

      • A

      • B

      Parameters

      • fa: HK<F, A>
      • fb: function
          • (): HK<F, B>
          • Returns HK<F, B>

      Returns HK<F, B>

forEffect

forEffect: function

Mixed-in from FlatMap.forEffect.

Type declaration

    • <A, B>(fa: HK<F, A>, fb: HK<F, B>): HK<F, A>
    • Type parameters

      • A

      • B

      Parameters

      • fa: HK<F, A>
      • fb: HK<F, B>

      Returns HK<F, A>

forEffectL

forEffectL: function

Mixed-in from FlatMap.forEffectL.

Type declaration

    • <A, B>(fa: HK<F, A>, fb: function): HK<F, A>
    • Type parameters

      • A

      • B

      Parameters

      • fa: HK<F, A>
      • fb: function
          • (): HK<F, B>
          • Returns HK<F, B>

      Returns HK<F, A>

map

map: function

Mixed-in from Applicative.map.

Type declaration

    • <A, B>(fa: HK<F, A>, f: function): HK<F, B>
    • Type parameters

      • A

      • B

      Parameters

      • fa: HK<F, A>
      • f: function
          • (a: A): B
          • Parameters

            • a: A

            Returns B

      Returns HK<F, B>

map2

map2: function

Mixed-in from Apply.map2.

Type declaration

    • <A, B, Z>(fa: HK<F, A>, fb: HK<F, B>, f: function): HK<F, Z>
    • Type parameters

      • A

      • B

      • Z

      Parameters

      • fa: HK<F, A>
      • fb: HK<F, B>
      • f: function
          • (a: A, b: B): Z
          • Parameters

            • a: A
            • b: B

            Returns Z

      Returns HK<F, Z>

product

product: function

Mixed-in from Apply.product.

Type declaration

    • <A, B>(fa: HK<F, A>, fb: HK<F, B>): HK<F, [A, B]>
    • Type parameters

      • A

      • B

      Parameters

      • fa: HK<F, A>
      • fb: HK<F, B>

      Returns HK<F, [A, B]>

recover

recover: function

Mixed-in from ApplicativeError.recover.

Type declaration

    • <A>(fa: HK<F, A>, f: function): HK<F, A>
    • Type parameters

      • A

      Parameters

      • fa: HK<F, A>
      • f: function
          • (e: E): A
          • Parameters

            • e: E

            Returns A

      Returns HK<F, A>

unit

unit: function

Mixed-in from Applicative.unit.

Type declaration

    • (): HK<F, void>
    • Returns HK<F, void>

Methods

flatMap

  • flatMap<A, B>(fa: HK<F, A>, f: function): HK<F, B>
  • Inherited from FlatMap.flatMap.

    Type parameters

    • A

    • B

    Parameters

    • fa: HK<F, A>
    • f: function
        • (a: A): HK<F, B>
        • Parameters

          • a: A

          Returns HK<F, B>

    Returns HK<F, B>

pure

  • pure<A>(a: A): HK<F, A>

raise

  • raise<A>(e: E): HK<F, A>

recoverWith

  • recoverWith<A>(fa: HK<F, A>, f: function): HK<F, A>

tailRecM

  • tailRecM<A, B>(a: A, f: function): HK<F, B>
  • Inherited from FlatMap.tailRecM.

    Type parameters

    • A

    • B

    Parameters

    • a: A
    • f: function
        • (a: A): HK<F, Either<A, B>>
        • Parameters

          • a: A

          Returns HK<F, Either<A, B>>

    Returns HK<F, B>

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc