samedi 4 avril 2020

Comparing two objects with default IComparable implementation with F# Reflection

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.





Aucun commentaire:

Enregistrer un commentaire