我知道对于派生 Data.Data 的数据类型, constrFields
给出字段名称列表.查看 GHC.Generics 文档,我认为 Generic
应该是一样的。也是。 (但悲惨地无法弄清楚自己该怎么做)。
更具体地说,我正在寻找两件事:
列出所有记录字段
...在 Haskell 程序中。我知道aeson能够自动推断派生 Generic
的任何记录数据类型的 JSON 表示,但阅读它的源代码只能证实我在这里一无所知。据我猜测,aeson 必须能够从记录数据类型中获取所有字段名称(如 String
或 ByteString
),以及它们的类型(其类型类似于 TypeRep
Data.Typeable,或 Eq
的一个实例:任何可用于 case
block 模式匹配的东西都可以)。
我隐约假设为 M1
创建一个类和实例, :*:
等是方法,但我无法进入类型检查器。
检查记录选择器
获取它所属的记录数据类型,记录字段名称(如 String
)等。
例如,给定
data Record = Record
{ recordId :: Int32
, recordName :: ByteString
} deriving Generic
一个函数
magic
这就像typeOf (Record {}) == typeOf (magic recordId)
deriving Generic
有可能吗? ,还是我必须求助于 Template Haskell?
最佳答案
列出所有记录字段
这个是很有可能的,确实是在Rep
的结构上递归完成的,使用一个类。以下解决方案适用于单构造函数类型,并为没有选择器的字段返回空字符串名称:
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
import Data.ByteString (ByteString)
import Data.Data
import Data.Int
import Data.Proxy
import GHC.Generics
import qualified Data.ByteString as B
data Record = Record { recordId :: Int32, recordName :: ByteString }
deriving (Generic)
class Selectors rep where
selectors :: Proxy rep -> [(String, TypeRep)]
instance Selectors f => Selectors (M1 D x f) where
selectors _ = selectors (Proxy :: Proxy f)
instance Selectors f => Selectors (M1 C x f) where
selectors _ = selectors (Proxy :: Proxy f)
instance (Selector s, Typeable t) => Selectors (M1 S s (K1 R t)) where
selectors _ =
[ ( selName (undefined :: M1 S s (K1 R t) ()) , typeOf (undefined :: t) ) ]
instance (Selectors a, Selectors b) => Selectors (a :*: b) where
selectors _ = selectors (Proxy :: Proxy a) ++ selectors (Proxy :: Proxy b)
instance Selectors U1 where
selectors _ = []
现在我们可以拥有:
selectors (Proxy :: Proxy (Rep Record))
-- [("recordId",Int32),("recordName",ByteString)]
这里最不明显的部分是
selName
和 Selector
: 这个类可以在 GHC.Generics
中找到,它允许我们从生成的选择器类型中提取选择器名称。在 Record
的情况下,表示是:kind! Rep Record
Rep Record :: * -> *
= D1
Main.D1Record
(C1
Main.C1_0Record
(S1 Main.S1_0_0Record (Rec0 Int32)
:*: S1 Main.S1_0_1Record (Rec0 ByteString)))
选择器类型为
Main.S1_0_0Record
和 Main.S1_0_1Record
.我们只能通过从 Rep
中提取这些类型来访问它们。使用类或类型族键入,因为 GHC 不导出它们。无论如何,selName
从任何 M1
中获取选择器名称带有 s
的节点选择器标签(它有一个更通用的类型t s f a -> String
,但这与我们无关)。也可以处理多个构造函数,并且有
selectors
返回 [[(String, TypeRep)]]
.在这种情况下,我们可能会有两个类,一个类似于上面的类,用于从给定的构造函数中提取选择器,另一个类用于收集构造函数的列表。检查记录选择器
从函数中获取记录类型很容易:
class Magic f where
magic :: f -> TypeRep
instance Typeable a => Magic (a -> b) where
magic _ = typeOf (undefined :: a)
或静态:
type family Arg f where
Arg (a -> b) = a
但是,如果没有 TH,我们就无法知道一个函数是合法的选择器还是只是具有正确类型的函数;它们在 Haskell 中无法区分。无法检查
magic recordId
中的名称“recordId” .2019更新 : 使用 GHC 8.6.5 提取选择器并输入
TypeRep
s。我们通过摆脱代理以支持类型应用程序来使解决方案现代化一点。{-# language
AllowAmbiguousTypes,
DeriveGeneric,
FlexibleContexts,
FlexibleInstances,
RankNTypes,
TypeApplications,
TypeInType
#-}
import Type.Reflection
import GHC.Generics
class Selectors rep where
selectors :: [(String, SomeTypeRep)]
instance Selectors f => Selectors (M1 D x f) where
selectors = selectors @f
instance Selectors f => Selectors (M1 C x f) where
selectors = selectors @f
instance (Selector s, Typeable t) => Selectors (M1 S s (K1 R t)) where
selectors =
[(selName (undefined :: M1 S s (K1 R t) ()) , SomeTypeRep (typeRep @t))]
instance (Selectors a, Selectors b) => Selectors (a :*: b) where
selectors = selectors @a ++ selectors @b
instance Selectors U1 where
selectors = []
现在用法变成了
selectors @(Rep MyType)
.
关于haskell - 是否可以在派生 Generic 的记录数据类型中列出字段的名称和类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27815489/