
On 6 jan 2009, at 18:08, Jeff Heard wrote:
Alright... I *think* I'm nearly there, but I can't figure out how to derive a class instance using record accessors and updaters... Can anyone help? There are [| XXXf |] instances at the end of the module and they all need replaced, but I can't figure out what to replace them with.
...
-- usage: $(deriveUIState ''MyTypeWithUIState) {- - Derive an instance of UIState from some type that has had UIState fields added to it. -} deriveUIState tp = do return [InstanceD [] (appUIState $ appType tp []) [FunD 'mousePosition [| mousePositionf |] ... ,FunD 'setMousePosition [| \b a -> a{ mousePositionf=b } |] ...
Quick guess: this doesn't typecheck? FunD :: Name -> [Clause] -> Dec while [| ... |] will return something of type ExpQ (which is the same as Q Exp). You're indeed nearly there, but if you use the quotation brackets you need to write monadic code (for the Q monad) and use functions like clause and funD. The tutorials on the wiki (you've probably seen them, http://www.haskell.org/haskellwiki/Template_Haskell) or pretty good and you could also look at packages at hackage for inspiration/ examples, e.g. http://hackage.haskell.org/packages/archive/haxr-th/3000.0.0/doc/html/src/Ne... -- Regards, Eelco Lempsink