I have this pSizeOf
function which takes a type param and returns the size of that type as type-level literal:
import Foreign.Storable
import Language.Haskell.TH
import qualified Data.Kind as K
pSizeOf :: forall (t :: K.Type). (Storable t) => TypeQ
pSizeOf = pure $ LitT $ NumTyLit $ fromIntegral $ sizeOf (undefined :: t)
I wonder is it possible to rewrite it without using type param t
, something of type
pSizeOf :: Name -> TypeQ
How can I get undefined
of that type from Name
? The following code:
pSizeOf :: Name -> TypeQ
pSizeOf n = pure $ LitT $ NumTyLit $ fromIntegral $ sizeOf $( SigE (VarE 'undefined) (ConT n))
fails with error:
* GHC stage restriction:
`n' is used in a top-level splice, quasi-quote, or annotation,
and must be imported, not defined locally
* In the untyped splice: $(SigE (VarE 'undefined) (ConT n))