简体   繁体   中英

F# / FSharp Generic Types with Comparison for Binary Tree from wikibooks.org

I'm trying to implement a basic Red/Black tree in Fsharp, based public code ( http://en.wikibooks.org/wiki/F_Sharp_Programming/Advanced_Data_Structures )

But I keep running into problems at compile time with the final signature:

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)

Specifically it gives me an error that "A type parameter is missing a constraint 'a when: comparison'" but every thing I've tried to add it hasn't worked. What am I missing?

Try re-write a type declaration as

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

I think there is no way to specify constraint on type parameter when you are using 'prefix' generic notation in class declaration

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM