示例代码:
{-# LANGUAGE NamedFieldPuns #-}
module Sample where
class Sample a where
isA :: a -> Bool
isB :: a -> Bool
isC :: a -> Bool
data X =
X
instance Sample X where
isA = undefined
isB = undefined
isC = undefined
data Wrapper = Wrapper
{ x :: X
, i :: Int
}
instance Sample Wrapper where
isA Wrapper {x} = isA x
isB Wrapper {x} = isB x
isC Wrapper {x} = isC x
在这里,我有一些由 实现的类,X然后是另一个Wrapper包含X.
我想通过它的字段Wrapper派生Sample实例x。
我知道我可以通过获取字段并为每个函数自己调用它来做到这一点,如图所示。
是否有一些标志或某种方法可以自动或仅执行一次?
这似乎类似于DerivingViaand GeneralisedNewtypeDeriving,但两者似乎newtype都只针对或强制类型