例如,假设我们有以下数据结构:
data Foo = Bool Bool | Int Int | Double Double
现在,有没有更简单的方法来做到这一点:
foo :: Typeable a => a -> Foo
foo x = maybe (error "i dunno") id $
liftM Bool (cast x) `mplus`
liftM Int (cast x) `mplus`
liftM Double (cast x)
有人想过为Typeable类型的模式匹配制定语法吗?