title: How to define a type in a TemplateHaskell function and use it in the same function?
body: <p>Is there any way to have as single TH function, define a type, and use the type, as well? Relevant code below. <code>PersonPoly2</code> is being defined by <code>makeRecordSplice</code> and then being passed to <code>makeAdaptorAndInstance</code> (from Opalaye), which is also a TH function.</p>
  
  <pre><code>{-# LANGUAGE FlexibleContexts       #-}
  {-# LANGUAGE FlexibleInstances      #-}
  {-# LANGUAGE FunctionalDependencies #-}
  {-# LANGUAGE MultiParamTypeClasses  #-}
  {-# LANGUAGE OverloadedStrings      #-}
  {-# LANGUAGE TemplateHaskell        #-}
  
  module Lib where
  
  import           Data.Profunctor.Product.TH             (makeAdaptorAndInstance)
  import Language.Haskell.TH
  
  makeRecordSplice :: Q [Dec]
  makeRecordSplice = [d|
    data PersonPoly2 a b = Person2
      { id :: a 
      , name :: b
      }
    |]
  
  makeRecordAndAdapter :: Q [Dec]
  makeRecordAndAdapter = do
    record &lt;- makeRecordSplice
    adapter &lt;- makeAdaptorAndInstance "pPerson2" (mkName "PersonPoly2")
    return $ record ++ adapter
  
  
  -------------
  
  /home/Projects/scratch/app/Main.hs:26:1: error:
      ‘PersonPoly2’ is not in scope at a reify
  Failed, modules loaded: Lib.
  </code></pre>