我想弄清楚类型类和GADTS之间的差异,特别是在使用-XMultiParamTypeClasses
扩展名时。了解何时使用类型类或GADT?
似乎都具有类似用途:
class MyClass a b where
f :: a -> b -> Bool
instance MyClass String String where
f s1 s2 = ...
instance MyClass Int Int where
f i1 i2 = ...
data Gadt a where
F :: String -> String -> Bool
F2 :: Int -> Int -> Bool
到目前为止,唯一的区别我真正看到的是,GADT的使能功能型接口有一个灵活的数字参数:
data Gadt a where
PassTwoArgs :: String -> String -> Gadt Bool
PassOneArgs :: String -> Gadt Bool
myFunction :: Gadt a -> a
myFunction (PassTwoArgs s1 s2) = ...
myFunction (PassOneArgs s1) = ...
虽然这对于类型类来说并不容易。
是否有任何其他差异或使用情况使用其中一种?