我有一些类和它们的实例。该示例显示了一些无意义的类。它们的确切性质并不重要。
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}
class Foo a where
foo :: a -> Int
class Bar a where
bar :: a -> String
instance Foo Int where
foo x = x
instance Foo String where
foo x = length x
instance Bar Int where
bar x = show x
instance Bar String where
bar x = x
Run Code Online (Sandbox Code Playgroud)
好的,现在我想创建一些存在类型,将这些类隐藏在某些数据类型外观后面,这样我就不必处理约束了。(我知道存在类型被认为是一种反模式,请不要向我解释这一点)。
data TFoo = forall a. Foo a => TFoo a …Run Code Online (Sandbox Code Playgroud)