haskell - 如何链接到类型类函数的特定实现?
问题描述
我正在为一个类型类编写文档,我想在其中指出一个现有的类型类实现,以给出一个如何编写自己的实例的示例。这是我正在尝试的:
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
,而且我也看不到链接到其源代码的方法。
解决方案
将该方法实现为普通旧函数的简单同义词,而不是就地编写实现。
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
推荐阅读
- android - Android Studio - EditText 输入移动 TextView
- c# - 从 Web 应用程序发送的电子邮件在项目符号中具有 >>
- sql-server - 使用实体框架存储浮点值引发异常
- android - 如何在由asset studio创建的android studio的asset studio中加载和重新编辑以前创建的图标文件?
- python - 有没有办法在 __init__ 中分配变量而不让它们立即运行?
- python - 如何使用 Blob 检测来隔离图像中的区域
- python - 熊猫数据框解决问题?
- google-apps-script - Apps 脚本,当 2 个值匹配时触发动作的难度
- node.js - 在 express 出现错误时自动运行 ejs-lint
- php - 添加带左尖括号的字符串会破坏我的数组