Comparing two objects with default IComparable implementation with F# Reflection

橙三吉。 提交于 2020-04-16 05:47:22

问题


Given two objects in F#, is there a way to use their IComparable method to compare them, assuming they are both of the same sub-type and that IComparable is implemented for their common sub-type.

What I am trying to achieve in pseudo-code :

    let tycompare (o1 : obj) (o2 : obj) : int option =
        let (ty1, ty2) = (o1.GetType(), o2.GetType())
        if ty1 <> ty2 then
            None
        else
            if IComparable is implemented on ty1 then
                o1.CompareTo(o2) |> Some
            else
                None

I am aware of this post but I do not think it helps answering my question directly.


回答1:


You could also write this more tersely with Option.bind, but this is a good fit for pattern matching.

We can define an active pattern for matching IComparable.

let (|IsComparable|) (obj : obj) = 
    match obj with
    | :? IComparable as comparable -> Some(comparable)
    | _ ->  None

F# lets you use active patterns in let bindings, so that the intent of function is more clearly conveyed.

let compare (IsComparable o1) (IsComparable o2) =        
    match (o1, o2) with
    | (Some o1, Some o2) when 
        o1.GetType() = o2.GetType() -> Some(o1.CompareTo(o2))
    | _ -> None

This can also be compacted (without the active-pattern - @kaefer) :

let compare (o1 : obj) (o2: obj) = 
    match (o1, o2 ) with
    | (:? System.IComparable as o1), (:? System.IComparable as o2) when 
        o1.GetType() = o2.GetType() -> Some(o1.CompareTo(o2))
    | _ -> None


来源:https://stackoverflow.com/questions/61025923/comparing-two-objects-with-default-icomparable-implementation-with-f-reflection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!