如何实现结构的比较?

时间:2017-08-12 12:53:25

标签: .net f#

[<Struct;CustomComparison>]
type Node<'a>(expr:'a, symbol:int) = 
    member x.Expression = expr
    member x.Symbol = symbol
    override x.ToString() = sprintf "%A" expr
    override x.GetHashCode() = symbol
    override x.Equals(y) = 
        match y with 
        | :? Node<'a> as y -> symbol = y.Symbol
        | _ -> failwith "Invalid equality for Node."

    interface IComparable with
        member x.CompareTo(y) = 
            match y with
            | :? Node<'a> as y -> compare symbol y.Symbol
            | _ -> failwith "Invalid comparison for Node."

以上操作不起作用,错误消息让我感到困惑。我正在使用Node类来保存AST中的其他节点,并且想知道是否将其转换为结构会加速我正在处理的编译器。

该类型在没有IComparable接口的情况下进行了类型检查,因此我特别询问了比较。

0 个答案:

没有答案