instance-map-0.1.0.0: Template haskell utilities for helping with deserialization etc. of existential types

Safe HaskellNone
LanguageHaskell2010

Type.InstanceMap.TH

Synopsis

Documentation

mkMap :: ClassName -> InputTypeName -> OutputWrapperName -> ExpQ -> Q [Dec] Source #

When used as a declaration splice, this function will create three declarations:

  • A data instance Some ClassName with a single constructor SomeClassName
  • A value named mapOfClassName, which has type
  Map TypeRep InputType -> Some Class
  • A helper function getSomeClassName with signature
  TypeRep -> InputType -> Maybe (Some Class)

The four arguments to this function are:

  1. A TypeRep indicating what monomorphic type we should try to decode
  2. The name of the type that is the argument to the decoder function. Often ''ByteString or ''Aeson.Value.
  3. The name of a monad type with an instance of MonadFail that the functional expression uses to wrap the return value. Often ''Maybe.
  4. A quasiquoted expression for a function that takes an argument of the type named by (2) and has a polymorphic return type wrapped in the monad (3).

For example, for JSON the call generally be:

$(mkMap ''MyClass ''Value ''Result [|fromJSON|])

mkMapWithOpts :: Options -> ClassName -> InputTypeName -> OutputWrapperName -> ExpQ -> Q [Dec] Source #

Like mkMap but with user-provided Options

data family Some (c :: * -> Constraint) Source #

data Options Source #

Constructors

Options 

Fields

  • maxDepth :: Int

    How deep to traverse the tree of instance contexts when attempting to find monomorphic types that are members of the class. Default is 2.

    For instance, if we have declarations like:

    data NumWithS n = NumWithS String n
    instance (Num a) => Num (NumWithS a)

    with maxDepth set to 2, then NumWithS Int and NumWithS (NumWithS Int) would both be found but NumWithS (NumWithS (NumWithS Int)) would not be.

  • verbose :: Bool

    Whether to produce lots of debugging output. Default False.

  • witnessGenerator :: ExpQ
     
  • witnessTypeName :: Name