newtype Const a b = Const { getConst :: a }
instance Functor (Const a) where
fmap f c = c
同 Identity Functor 一样 , (Const a)
也是一个 Functor 的实例。不同的是 Const a
这个盒子中存在一个幻影类型 b
。代表了盒子抽象的另一个极 端,是个什么都没有装的盒子。盒子中 Const a b
的类型 b
{.verbatim} 可以通过 fmap
被转换为任何其它类型 c
。因为我们并不关心盒子当中存的是什么。