我想做的是这样的:
data MyDataType = MyDataType
class HasSpecialField a where
specialField :: a -> ByteString
instance HasSpecialField a => HasSpecialField (a, b) where
specialField (a, b) = specialField a
instance HasSpecialField b => HasSpecialField (a, b) where
specialField (a, b) = specialField b
instance HasSpecialField MyDataType where
specialField _ = "Coolio"
也就是说,如果你有一个像这样的元组:
myTuple :: (String, (Int, MyDataType))
然后您可以运行specialField myTuple
返回“Coolio”。
有没有办法做到这一点?