Allow instance Typeable in withStatic blocks

`Typeable` instances are not user defined. But the user nevertheless
sometimes needs static evidence of typeability. The solution is to
allow users to write

```haskell
withStatic [d| instance Typeable T |]
```

for any datatype `T`. `withStatic` will generate static evidence, but
won't pass through the instance declaration itself, since it is
illegal.
This commit is contained in:
Mathieu Boespflug 2018-09-12 22:06:28 +02:00
parent 793ee0627d
commit 6059d410c4
2 changed files with 9 additions and 3 deletions

View File

@ -82,7 +82,7 @@ double = (*2)
newtype SerializableInt = SI Int deriving (Generic, Typeable)
withStatic [d|
instance Binary SerializableInt
instance Serializable SerializableInt
instance Typeable SerializableInt
|]
-- | Demonstration of client server interactions.

View File

@ -92,7 +92,9 @@ mangleName name@(TH.Name occ fl) = case fl of
-- @
--
-- You will probably want to enable @FlexibleContexts@ and @ScopedTypeVariables@
-- in modules that use 'withStatic'.
-- in modules that use 'withStatic'. 'withStatic' can also handle non-user
-- generated instances like 'Typeable' instances: just write @instance Typeable
-- T@.
withStatic :: TH.DecsQ -> TH.DecsQ
withStatic = (>>= go)
where
@ -140,5 +142,9 @@ withStatic = (>>= go)
let staticins = TH.InstanceD staticcxt statichd methods
#endif
decls' <- go decls
return (ins : sigf : declf : staticins : decls')
case hd of
TH.AppT (TH.ConT nm) _ | nm == ''Typeable ->
return (sigf : declf : staticins : decls')
_ ->
return (ins : sigf : declf : staticins : decls')
go (decl:decls) = (decl:) <$> go decls