0 people like it.

A template for projection comparison

An abstract class that implements all the boilerplate required for custom comparison semantics.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
open System

type ProjectionComparison<'Id, 'Cmp when 'Cmp : comparison> (token : 'Cmp) =
    member __.ComparisonToken = token

    interface IComparable with
        member x.CompareTo y =
            match y with
            | :? ProjectionComparison<'Id, 'Cmp> as y -> compare token y.ComparisonToken
            | _ -> invalidArg "y" "invalid comparand."

    override x.Equals y =
        match y with
        | :? ProjectionComparison<'Id, 'Cmp> as y -> token = y.ComparisonToken
        | _ -> false

    override x.GetHashCode() = hash token

// example

type ComparableType(t : Type) =
    inherit ProjectionComparison<ComparableType, string>(t.AssemblyQualifiedName)
    member __.Type = t


ComparableType(typeof<int>) < ComparableType(typeof<int>)
namespace System
Multiple items
type ProjectionComparison<'Id,'Cmp (requires comparison)> =
  interface IComparable
  new : token:'Cmp -> ProjectionComparison<'Id,'Cmp>
  override Equals : y:obj -> bool
  override GetHashCode : unit -> int
  member ComparisonToken : 'Cmp

Full name: Script.ProjectionComparison<_,_>

--------------------
new : token:'Cmp -> ProjectionComparison<'Id,'Cmp>
val token : 'Cmp (requires comparison)
member ProjectionComparison.ComparisonToken : 'Cmp

Full name: Script.ProjectionComparison`2.ComparisonToken
Multiple items
type IComparable =
  member CompareTo : obj:obj -> int

Full name: System.IComparable

--------------------
type IComparable<'T> =
  member CompareTo : other:'T -> int

Full name: System.IComparable<_>
val x : ProjectionComparison<'Id,'Cmp> (requires comparison)
override ProjectionComparison.CompareTo : y:obj -> int

Full name: Script.ProjectionComparison`2.CompareTo
val y : obj
val y : ProjectionComparison<'Id,'Cmp> (requires comparison)
val compare : e1:'T -> e2:'T -> int (requires comparison)

Full name: Microsoft.FSharp.Core.Operators.compare
property ProjectionComparison.ComparisonToken: 'Cmp
val invalidArg : argumentName:string -> message:string -> 'T

Full name: Microsoft.FSharp.Core.Operators.invalidArg
override ProjectionComparison.Equals : y:obj -> bool

Full name: Script.ProjectionComparison`2.Equals
override ProjectionComparison.GetHashCode : unit -> int

Full name: Script.ProjectionComparison`2.GetHashCode
val hash : obj:'T -> int (requires equality)

Full name: Microsoft.FSharp.Core.Operators.hash
Multiple items
type ComparableType =
  inherit ProjectionComparison<ComparableType,string>
  new : t:Type -> ComparableType
  member Type : Type

Full name: Script.ComparableType

--------------------
new : t:Type -> ComparableType
val t : Type
type Type =
  inherit MemberInfo
  member Assembly : Assembly
  member AssemblyQualifiedName : string
  member Attributes : TypeAttributes
  member BaseType : Type
  member ContainsGenericParameters : bool
  member DeclaringMethod : MethodBase
  member DeclaringType : Type
  member Equals : o:obj -> bool + 1 overload
  member FindInterfaces : filter:TypeFilter * filterCriteria:obj -> Type[]
  member FindMembers : memberType:MemberTypes * bindingAttr:BindingFlags * filter:MemberFilter * filterCriteria:obj -> MemberInfo[]
  ...

Full name: System.Type
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
Multiple items
member ComparableType.Type : Type

Full name: Script.ComparableType.Type

--------------------
type Type =
  inherit MemberInfo
  member Assembly : Assembly
  member AssemblyQualifiedName : string
  member Attributes : TypeAttributes
  member BaseType : Type
  member ContainsGenericParameters : bool
  member DeclaringMethod : MethodBase
  member DeclaringType : Type
  member Equals : o:obj -> bool + 1 overload
  member FindInterfaces : filter:TypeFilter * filterCriteria:obj -> Type[]
  member FindMembers : memberType:MemberTypes * bindingAttr:BindingFlags * filter:MemberFilter * filterCriteria:obj -> MemberInfo[]
  ...

Full name: System.Type
val typeof<'T> : Type

Full name: Microsoft.FSharp.Core.Operators.typeof
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>

More information

Link:http://fssnip.net/h7
Posted:4 years ago
Author:Eirik Tsarpalis
Tags: comparison , object oriented