繁体   English   中英

由多态实例实例化的Typeclass

[英]Typeclass instantiated by Polymorphic instances

我想重用如下代码:

instance ToJavascript j => YesodTemplate j where toBuilder = toJavascript

这需要使用包装的实例等进行重叠。因此,我看到的解决方案是使用包装器。

newtype Wrapped a = Wrapped { unwrapped :: using a }
instance ToJavascript j => YesodTemplate (Wrapped j) j where
  toBuilder = toJavascript

我在那里有多余的未包装的j,因此具有默认函数的数据声明,我可以将模板类写为

class YesodTemplate yt inner where
  toBuilder :: inner -> Builder
  file :: (inner -> yt) -> FilePath -> Q Exp
  file wrap fp = readFileQ fp >>= stringToTH wrap

包装功能是满足类型系统的虚拟对象。 但这仍然无法编译。

juliusFile :: FilePath -> Q Exp juliusFile = file (Wrapped :: ToJavascript j => j -> Wrapped j) Ambiguous type variable `inner1' in the constraint: (ToJavascript inner1) arising from an expression type signature Probable fix: add a type signature that fixes these type variable(s) In the first argument one of `file', namely `(Wrapped :: ToJavascript j => j -> Wrapped j)' In the expression: file (Wrapped :: ToJavascript j => j -> Wrapped j) In an equation for `juliusFile': juliusFile = file (Wrapped :: ToJavascript j => j -> Wrapped j)

感谢大家的帮助和鼓励!

我最后要做的而不是使用包装的实例,而是使用具有默认功能的数据声明,该默认功能可以被覆盖。 这种方法更加干净,因为它没有包装器,也没有伪变量-我认为Stephen正确的是,原始方法并不是为haskell / GHC设计的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM