Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The type should be available, along with everything else, in the <code>Info</code> value provided by <code>reify</code>. Specifically, you should get a <code>TyConI</code>, which contains <a href="http://hackage.haskell.org/packages/archive/template-haskell/2.6.0.0/doc/html/Language-Haskell-TH.html#t%3aDec">a <code>Dec</code> value</a>, from which you can get the list of <a href="http://hackage.haskell.org/packages/archive/template-haskell/2.6.0.0/doc/html/Language-Haskell-TH.html#t%3aCon"><code>Con</code> values specifying the constructors</a>. A record type should then use <code>RecC</code>, which will give you a list of fields <a href="http://hackage.haskell.org/packages/archive/template-haskell/2.6.0.0/doc/html/Language-Haskell-TH-Syntax.html#t%3aVarStrictType">described by a tuple</a> containing the field name, whether the field is strict, and <a href="http://hackage.haskell.org/packages/archive/template-haskell/2.6.0.0/doc/html/Language-Haskell-TH-Syntax.html#t%3aType">the type</a>.</p> <p>Where you go from there depends on what you want to do with all this.</p> <hr> <p><strong>Edit</strong>: For the sake of actually demonstrating the above, here's a really terrible quick and dirty function that finds record fields:</p> <pre><code>import Language.Haskell.TH test :: Name -&gt; Q Exp test n = do rfs &lt;- fmap getRecordFields $ reify n litE . stringL $ show rfs getRecordFields :: Info -&gt; [(String, [(String, String)])] getRecordFields (TyConI (DataD _ _ _ cons _)) = concatMap getRF' cons getRecordFields _ = [] getRF' :: Con -&gt; [(String, [(String, String)])] getRF' (RecC name fields) = [(nameBase name, map getFieldInfo fields)] getRF' _ = [] getFieldInfo :: (Name, Strict, Type) -&gt; (String, String) getFieldInfo (name, _, ty) = (nameBase name, show ty) </code></pre> <p>Importing that in another module, we can use it like so:</p> <pre><code>data Foo = Foo { foo1 :: Int, foo2 :: Bool } foo = $(test ''Foo) </code></pre> <p>Loading that in GHCi, the value in <code>foo</code> is <code>[("Foo",[("foo1","ConT GHC.Types.Int"),("foo2","ConT GHC.Types.Bool")])]</code>.</p> <p>Does that give you the rough idea?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload