ema-generics-0.10.0.0: Static site generator library with hot reload
Safe HaskellSafe-Inferred
LanguageHaskell2010

Ema.Route.Generic.TH

Synopsis

Main TH

deriveIsRoute :: Name -> TypeQ -> Q [Dec] Source #

deriveIsRoute route model subroutes derives HasSubRoutes, HasSubModels, and IsRoute for the given route.

Subroutes are optionally supplied, but if they are then the length of the list must be the same as the number of constructors in route.

TODO: Add TypeErrors to catch mismatched WithSubRoutes list shapes at the generic deriving level?

Convenient re-exports

deriveGeneric :: Name -> Q [Dec] #

Generate generics-sop boilerplate for the given datatype.

This function takes the name of a datatype and generates:

Note that the generated code will require the TypeFamilies and DataKinds extensions to be enabled for the module.

Example: If you have the datatype

data Tree = Leaf Int | Node Tree Tree

and say

deriveGeneric ''Tree

then you get code that is equivalent to:

instance Generic Tree where

  type Code Tree = '[ '[Int], '[Tree, Tree] ]

  from (Leaf x)   = SOP (   Z (I x :* Nil))
  from (Node l r) = SOP (S (Z (I l :* I r :* Nil)))

  to (SOP    (Z (I x :* Nil)))         = Leaf x
  to (SOP (S (Z (I l :* I r :* Nil)))) = Node l r
  to (SOP (S (S x)))                   = x `seq` error "inaccessible"

instance HasDatatypeInfo Tree where
  type DatatypeInfoOf Tree =
    T.ADT "Main" "Tree"
      '[ T.Constructor "Leaf", T.Constructor "Node" ]

  datatypeInfo _ =
    T.demoteDatatypeInfo (Proxy :: Proxy (DatatypeInfoOf Tree))

Limitations: Generation does not work for GADTs, for datatypes that involve existential quantification, for datatypes with unboxed fields.