如何表达具有返回相同类型类值的函数的类型类
How to express typeclass that has a function returning a value of the same typeclass
我有以下纯脚本代码:
class Node a where
parentNode :: forall b. (Node b) => a -> b
但是编译时出现以下错误:
A cycle appears in the definition of type synonym Node
Cycles are disallowed because they can lead to loops in the type checker.
Consider using a 'newtype' instead.
我正在尝试编写一个 parentNode
函数 returns 一个节点的父节点。父节点的唯一保证是它也是一个Node b
。
我不关心 b
的实际类型是什么。
我基本上是想说,parentNode
应该是一个函数,returns 一个值也实现了 Node 类型类。类型 类 是否可以实现类似的事情,或者是否有其他惯用的方法来做这种事情?
你的parentNode
函数的类型说调用者可以选择父类的b
类型,但我认为这是不正确的。类型应由 DOM 中的内容决定,因此您需要一个存在类型。
这里的技术问题是类型 类 目前不能引用它们自己。
但是,在这种情况下,我认为有一个不使用 类 的更简单的解决方案。为什么不是这样的?
class IsNode a where
toNode :: a -> Node
foreign import data Node :: *
foreign import parentNode :: Node -> Node
我有以下纯脚本代码:
class Node a where
parentNode :: forall b. (Node b) => a -> b
但是编译时出现以下错误:
A cycle appears in the definition of type synonym Node
Cycles are disallowed because they can lead to loops in the type checker.
Consider using a 'newtype' instead.
我正在尝试编写一个 parentNode
函数 returns 一个节点的父节点。父节点的唯一保证是它也是一个Node b
。
我不关心 b
的实际类型是什么。
我基本上是想说,parentNode
应该是一个函数,returns 一个值也实现了 Node 类型类。类型 类 是否可以实现类似的事情,或者是否有其他惯用的方法来做这种事情?
你的parentNode
函数的类型说调用者可以选择父类的b
类型,但我认为这是不正确的。类型应由 DOM 中的内容决定,因此您需要一个存在类型。
这里的技术问题是类型 类 目前不能引用它们自己。
但是,在这种情况下,我认为有一个不使用 类 的更简单的解决方案。为什么不是这样的?
class IsNode a where
toNode :: a -> Node
foreign import data Node :: *
foreign import parentNode :: Node -> Node