首页 > 解决方案 > 如何链接到类型类函数的特定实现?

问题描述

我正在为一个类型类编写文档,我想在其中指出一个现有的类型类实现,以给出一个如何编写自己的实例的示例。这是我正在尝试的:

class Something a where

  -- | Please take a look at 'MyModule.complicatedFunction' for an 
  -- example of how to write this function for your own implementations
  -- of this type-class
  complicatedFunction :: a -> Int

生成的函数确实链接到MyModule,但它没有链接到 MyModule 的实现complicatedFunction,而且我也看不到链接到其源代码的方法。

标签: haskellhaddock

解决方案


将该方法实现为普通旧函数的简单同义词,而不是就地编写实现。

module MyModule where

data MyType = MyConstructor

complicatedFunction_MyType :: MyType -> Int
complicatedFunction_MyType MyConstructor = 734

module YourClass where

class Something a where

  -- | Please take a look at 'MyModule.complicatedFunction_MyType' for an 
  -- example of how to write this function for your own implementations
  -- of this type-class
  complicatedFunction :: a -> Int

instance Something MyType where
  complicatedFunction = complicatedFunction_MyType

推荐阅读