如何链接到类型类函数的特定实现?

时间:2018-07-28 07:28:57

标签: haskell haddock

我正在为类型类编写文档,在此我想指向现有的类型类实现,以提供有关如何编写自己的实例的示例。这是我正在尝试的:

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的实现,并且我看不到链接到它的源代码。

1 个答案:

答案 0 :(得分:1)

将方法作为普通旧函数的简单同义词来实现,而不是在原位编写实现。

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