假设我有这个:
data PT1
data PT2
data DT1 a = DT1 { field :: Int }
newtype DT2 a = DT2 (DT1 a)
f :: Int -> DT2 a -> Int
f x (DT2 (DT1 PT1 field)) = 5 -- How do I specify the type param?
f x (DT2 (DT1 PT2 field)) = 7 -- How do I specify the type param?
问题在评论里。上面的内容无法编译。我想根据类型参数进行不同的模式匹配。我该怎么做?
最佳答案
您无法对类型进行模式匹配。你可以做的是使用这样的类型类:
class Effable t where
f :: Int -> t -> Int
instance Effable (DT2 PT1) where
f x (DT2 _) = 5
instance Effable (DT2 PT2) where
f x (DT2 _) = 7
关于haskell - 如何在幻像类型的模式匹配中指定类型参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4909427/