我基本上是想看看我是否可以在 Haskell 中模拟 ORM 框架,这样如果用户想要制作数据库模型,他们会做这样的事情
data Car = Car {
company :: String,
model :: String,
year :: Int
} deriving (Model)
表格是“汽车”,列是公司、型号、年份
要在 Haskell 中执行此操作,您必须使用类和泛型的组合,这就是我遇到的问题。使用本教程(http://www.haskell.org/ghc/docs/7.4.1/html/users_guide/generic-programming.html),我想出了这个(基本上是复制和重命名,所以我可以得到代码工作)
{-# LANGUAGE DeriveGeneric, TypeOperators, TypeSynonymInstances, FlexibleInstances #-}
module Main where
import GHC.Generics
class SModel b where
s_new :: b -> IO()
instance SModel Int where
s_new s = putStrLn s++":Int"
instance SModel Integer where
s_new s = putStrLn s++":Integer"
instance SModel String where
s_new s = putStrLn s++":String"
class Model m where
new :: m a -> IO()
instance Model U1 where
new U1 = putStrLn "unit"
instance (Model a, Model b) => Model (a :*: b) where
new (a :*: b) = do
new a
new b
instance (Model a, Model b) => Model (a :+: b) where
new (L1 x) = new x
new (R1 x) = new x
instance (Model a) => Model (M1 i c a) where
new (M1 x) = new x
instance (SModel a) => Model (K1 i a) where
new (K1 x) = s_new x
data Car = Car {
company :: String,
model :: String,
year :: Int
} deriving (Model)
上面的代码会产生错误
Cannot derive well-kinded instance of form `Model (Car ...)'
Class `Model' expects an argument of kind `* -> *'
In the data declaration for `Car'
我有点卡在这一点上,我相信我已经实例化了所有必需的通用类型来覆盖记录