在处理类型族时,使用相等约束通常很方便,以避免必须在签名中重复某些类型函数的名称:
class Foo f where
type BulkyAssociatedType f :: *
foo :: BulkyAssociatedType f -> f
...
bar :: forall m f b .
( Monad m, Foo f, b ~ BulkyAssociatedType f
, Monoid b, Monoid (m b)
) => f -> m f
即使在签名本身中并没有出现缩写,也仅在约束中出现缩写,这才有效。
对于班级,这显然是不可能的。
class ( Foo f, b ~ BulkyAssociatedType f, Monoid b, ...) => Bar f
抱怨类型变量b
不在范围内。
是否有某种方法可以达到类似目的,从而避免出现一些重复样板?
得知您无法做到这一点令我感到惊讶(我使用了相同的技术,并且知道它可以在实例声明中使用),但是似乎存在着长期支持GHC功能的请求。
也许您可以ConstraintKinds
用来获得相同的好处:
{-# LANGUAGE TypeFamilies , FlexibleContexts , ConstraintKinds #-}
import Data.Monoid
class Foo f where
type BulkyAssociatedType f :: *
type B f = (Monoid (BulkyAssociatedType f))
class ( Foo f, B f) => Bar f
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句