<html>
  <head>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div style="" class="markdown-here-wrapper"
      data-md-url="Thunderbird">
      <p style="margin: 1.2em 0px ! important;">If your
        instance-generating function is defined in an external module,
        you can use it in GHCi like this:</p>
      <pre style="font-size: 0.85em; font-family: Consolas,Inconsolata,Courier,monospace;font-size: 1em; line-height: 1.2em;margin: 1.2em 0px;"><code style="font-size: 0.85em; font-family: Consolas,Inconsolata,Courier,monospace;margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid rgb(234, 234, 234); background-color: rgb(248, 248, 248); border-radius: 3px; display: inline;white-space: pre; overflow: auto; border-radius: 3px; border: 1px solid rgb(204, 204, 204); padding: 0.5em 0.7em; display: block ! important;display: block; padding: 0.5em; color: rgb(51, 51, 51); background: rgb(248, 248, 255) none repeat scroll 0% 0%;" class="hljs language-haskell"><span class="hljs-comment">-- Enable TH</span>
> :set -<span class="hljs-type">XTemplateHaskell</span> 

<span class="hljs-comment">-- Import modules (e.g. Aeson)</span>
> <span class="hljs-import"><span class="hljs-keyword">import</span> Data.Aeson.TH</span>
> <span class="hljs-import"><span class="hljs-keyword">import</span> Data.Aeson</span>

<span class="hljs-comment">-- Define the type for which I'm going to generate instances</span>
> <span class="hljs-typedef"><span class="hljs-keyword">data</span> <span class="hljs-type">X</span> = <span class="hljs-type">X</span> <span class="hljs-container">{<span class="hljs-title">x</span> :: <span class="hljs-type">Int</span>}</span></span>

<span class="hljs-comment">-- Derive the instance</span>
<span class="hljs-type">Prelude</span> <span class="hljs-type">Data</span>.<span class="hljs-type">Aeson</span>.<span class="hljs-type">TH</span>> <span class="hljs-typedef"><span class="hljs-keyword">data</span> <span class="hljs-type">Dummy</span>; deriveJSON defaultOptions ''<span class="hljs-type">X</span></span>

<span class="hljs-comment">-- Check that it works</span>
> toJSON (<span class="hljs-type">X</span> <span class="hljs-number">1</span>)
<span class="hljs-type">Object</span> (fromList [(<span class="hljs-string">"x"</span>,<span class="hljs-type">Number</span> <span class="hljs-number">1.0</span>)])
</code></pre>
      <p style="margin: 1.2em 0px ! important;">(More about the <code style="font-size: 0.85em; font-family: Consolas,Inconsolata,Courier,monospace;margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid rgb(234, 234, 234); background-color: rgb(248, 248, 248); border-radius: 3px; display: inline;">data Dummy</code>
        trick here: <a
href="https://www.reddit.com/r/haskelltil/comments/3ghacj/you_can_use_template_haskell_functions_like/"><a class="moz-txt-link-freetext" href="https://www.reddit.com/r/haskelltil/comments/3ghacj/you_can_use_template_haskell_functions_like/">https://www.reddit.com/r/haskelltil/comments/3ghacj/you_can_use_template_haskell_functions_like/</a></a>.)</p>
      <div
title="MDH:SWYgeW91ciBpbnN0YW5jZS1nZW5lcmF0aW5nIGZ1bmN0aW9uIGlzIGRlZmluZWQgaW4gYW4gZXh0ZXJuYWwgbW9kdWxlLCB5b3UgY2FuIHVzZSBpdCBpbiBHSENpIGxpa2UgdGhpczo8YnI+PGJyPn5+
fiBoYXNrZWxsPGJyPi0tIEVuYWJsZSBUSDxicj4mZ3Q7IDpzZXQgLVhUZW1wbGF0ZUhhc2tlbGwg
PGJyPjxicj4tLSBJbXBvcnQgbW9kdWxlcyAoZS5nLiBBZXNvbik8YnI+Jmd0OyBpbXBvcnQgRGF0
YS5BZXNvbi5USDxicj4mZ3Q7IGltcG9ydCBEYXRhLkFlc29uPGJyPjxicj4tLSBEZWZpbmUgdGhl
IHR5cGUgZm9yIHdoaWNoIEknbSBnb2luZyB0byBnZW5lcmF0ZSBpbnN0YW5jZXM8YnI+Jmd0OyBk
YXRhIFggPSBYIHt4IDo6IEludH08YnI+PGJyPi0tIERlcml2ZSB0aGUgaW5zdGFuY2U8YnI+UHJl
bHVkZSBEYXRhLkFlc29uLlRIJmd0OyBkYXRhIER1bW15OyBkZXJpdmVKU09OIGRlZmF1bHRPcHRp
b25zICcnWDxicj48YnI+LS0gQ2hlY2sgdGhhdCBpdCB3b3Jrczxicj4mZ3Q7IHRvSlNPTiAoWCAx
KTxicj5PYmplY3QgKGZyb21MaXN0IFsoIngiLE51bWJlciAxLjApXSk8YnI+fn5+PGJyPjxicj4o
TW9yZSBhYm91dCB0aGUgYGRhdGEgRHVtbXlgIHRyaWNrIGhlcmU6ICZsdDtodHRwczovL3d3dy5y
ZWRkaXQuY29tL3IvaGFza2VsbHRpbC9jb21tZW50cy8zZ2hhY2oveW91X2Nhbl91c2VfdGVtcGxh
        dGVfaGFza2VsbF9mdW5jdGlvbnNfbGlrZS8mZ3Q7Lik8YnI+PGJyPg=="
style="height:0;width:0;max-height:0;max-width:0;overflow:hidden;font-size:0em;padding:0;margin:0;">​</div>
    </div>
  </body>
</html>