Generating Eta code at compile-time
Metaprogramming provides a way to manipulate code as you would data. Eta provides a form of metaprogramming called template metaprogramming based on GHC's TemplateHaskell. Template metaprogramming allows you to instantiate code templates by supplying the appropriate parameters for a given situation.
In the example below, makeLenses is a template function from the lens package that is evaluated at compile-time and generates some code. The function takes a single parameter: a type.
1 2 3 4 5 6 | import Control.Lens
data Person = Person { _me :: String,
_age :: Double }
makeLenses ''Person
|
The code above will simplify to the code below once the template function makeLenses gets run during compilation.
1 2 3 4 5 6 7 8 9 10 11 12 | import Control.Lens
data Person = Person { _me :: String,
_age :: Double }
age :: Lens' Person Double
age f (Person x1 x2) = fmap (\x -> Person x1 x) (f x2)
{-# INLINE age #-}
me :: Lens' Person String
me f (Person x1 x2) = fmap (\x -> Person x x2) (f x1)
{-# INLINE me #-}
|
In the future, there will be more detailed documentation on the metaprogramming facilities offered by Eta. In the meantime, you can check out this link for a quick introduction to Template Haskell. Most of the code supplied should work the same as it does in Haskell.