似乎获取字段类型的一种方法是通过泛型表示:
ghci> :kind! Rep (Server API)
Rep (Server API) :: * -> *
= M1
D
('MetaData "Counter" "Main" "main" 'False)
(M1
C
('MetaCons "Counter" 'PrefixI 'True)
(M1
S
('MetaSel
('Just "counterPost")
'NoSourceUnpackedness
'NoSourceStrictness
'DecidedLazy)
(K1 R (Int -> Handler NoContent))
:*: M1
S
('MetaSel
('Just "counterGet")
'NoSourceUnpackedness
'NoSourceStrictness
'DecidedLazy)
(K1 R (Handler Int))))
有点冗长,但它能很好地与
"Eval" code lens
在VSCode中:
为了减少冗长
Generics
-基于helper可以生成更易于管理的输出。使用我的
by-other-names
包,我们可以定义:
recordFields ::
forall r.
(Generic r, GHasFieldNames (Rep r), GRecord Typeable (Rep r)) =>
[(String, TypeRep)]
recordFields =
Data.Foldable.toList $
gRecordEnum @Typeable @(Rep r) gGetFieldNames typeRep
哪个,投入使用: