7
|
schuelermine · 技术社区 · 6 年前 |
![]() |
1
3
这个
要在没有GHC扩展的情况下消除歧义,可以使用代理传递或基于新类型的标记。我相信这两种技术都是由Edward Kmett和Shachaf Ben Kiki的最后一个多态性自旋给出的(参见
Who invented proxy passing and when?
). 代理传递倾向于提供一个易于使用的API,而newtype方法在某些情况下更有效。下面是代理传递方法。这要求您传递某种类型的参数。传统上,调用者会使用
下面是使用代理传递时类的外观:
下面是课堂的样子:
下面是您如何使用它:
|
![]() |
2
11
您建议的非函数方法将具有
不明确的类型
*Main> floatDigits' :: Int <interactive>:3:1: error: ⢠No instance for (RealFloat' a0) arising from a use of âfloatDigits'â ⢠In the expression: floatDigits' :: Int In an equation for âitâ: it = floatDigits' :: Int *Main> floatDigits' :: RealFloat Double => Int <interactive>:4:1: error: ⢠Could not deduce (RealFloat' a0) arising from a use of âfloatDigits'â from the context: RealFloat Double bound by an expression type signature: RealFloat Double => Int at :4:17-39 The type variable âa0â is ambiguous ⢠In the expression: floatDigits' :: RealFloat Double => Int In an equation for âitâ: it = floatDigits' :: RealFloat Double => Int 因此,Haskell首先不允许您编写类型不明确的方法。实际上,当我在上面编写类时尝试编译该类时,会出现以下错误消息: ⢠Could not deduce (RealFloat' a0) from the context: RealFloat' a bound by the type signature for: floatDigits' :: forall a. RealFloat' a => Int at /tmp/wtmpf-file3738.hs:2:3-21 The type variable âa0â is ambiguous ⢠In the ambiguity check for âfloatDigits'â To defer the ambiguity check to use sites, enable AllowAmbiguousTypes When checking the class method: floatDigits' :: forall a. RealFloat' a => Int In the class declaration for âRealFloat'â
但是,当实例无法在使用站点解析时,有什么意义呢?好吧,是的 可以 实际上可以解决,但只能使用 another pretty new GHC extension :
|
![]() |
3
5
问题在于,您需要为多个实例构造函数,例如:
但它认为参数的值并不重要,例如:
|