相关疑难解决方法(0)

试图抽象出类型类,但类型变量转义

我有一些类和它们的实例。该示例显示了一些无意义的类。它们的确切性质并不重要。

{-# 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)

haskell types typeclass

2
推荐指数
1
解决办法
75
查看次数

标签 统计

haskell ×1

typeclass ×1

types ×1