来自wikibooks.org的二叉树比较的F#/ FSharp通用类型

时间:2014-09-08 17:55:12

标签: generics f#

我试图在Fsharp中实现一个基本的红/黑树,基于公共代码(http://en.wikibooks.org/wiki/F_Sharp_Programming/Advanced_Data_Structures

但是我在编译时遇到了最终签名的问题:

type 'a BinaryTree(inner : 'a tree) =
    member this.head = Tree.head inner
    member this.left = BinaryTree(Tree.left inner)
    member this.right = BinaryTree(Tree.right inner)
    member this.exists item = Tree.exists item inner
    member this.insert item = BinaryTree(Tree.insert item inner)
    member this.print() = Tree.print 0 inner
    static member empty = BinaryTree<'a>(E)

具体来说,它给了我一个错误&#34;一个类型参数缺少一个约束&a; a when:comparison&#39;&#34;但我尝试添加它的每件事都没有奏效。我错过了什么?

1 个答案:

答案 0 :(得分:5)

尝试将类型声明重写为

type BinaryTree<'a when 'a : comparison> (inner : 'a tree) =

我认为当您使用&#39;前缀&#39;时,无法指定类型参数的约束。类声明中的通用符号