自动导出PathPiece

时间:2015-02-21 10:09:52

标签: haskell yesod

在我的Yesod网络应用程序中,我有以下数据类型:

data SensorType = TemperatureSensor | HumiditySensor deriving (Eq, Show, Read, PathPiece, PersistField)

我启用GeneralizedNewtypeDeriving

关于自动导出PathPiece的答案:

What typeclasses need to be defined for a Yesod path?

但是我得到了ghc-error:

Can't make a derived instance of `PathPiece SensorType
  `PathPiece' is not a derivable class

是否可以自动导出PathPiece?我做错了什么?

{0}存在PersistField。那么derivePersistField呢?

2 个答案:

答案 0 :(得分:2)

顾名思义,

GeneralizedNewtypeDeriving仅对导出newtype的实例有用。它通过在newtype包装器上提升底层类型的实例来工作。

您的类型不是newtype,因此无法解除PathPiece个实例。

答案 1 :(得分:1)

根据path-pieces package没有做类似的事情或我能找到的任何代码on Github,它看起来不太可能。我会像这样实现PathPiece类型类(使用优等前奏中的tshowreadMay):

instance PathPiece SensorType where
    toPathPiece = tshow
    fromPathPiece = readMay
相关问题