[Haskell-cafe] Is there a way to generate instances from the GHCi REPL?

Artyom yom at artyom.me
Thu Feb 4 22:09:19 UTC 2016


If your instance-generating function is defined in an external module, 
you can use it in GHCi like this:

|-- Enable TH > :set -XTemplateHaskell -- Import modules (e.g. Aeson) > 
import Data.Aeson.TH > import Data.Aeson -- Define the type for which 
I'm going to generate instances > data X = X {x :: Int} -- Derive the 
instance Prelude Data.Aeson.TH> data Dummy; deriveJSON defaultOptions 
''X -- Check that it works > toJSON (X 1) Object (fromList [("x",Number 
1.0)]) |

(More about the |data Dummy| trick here: 
https://www.reddit.com/r/haskelltil/comments/3ghacj/you_can_use_template_haskell_functions_like/.)

​
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20160205/a0e079fb/attachment.html>


More information about the Haskell-Cafe mailing list