1 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: 
27: 
28: 
open System

[<AbstractClass>]
type ProjectionComparison<'Id, 'Cmp when 'Cmp : comparison> () =
    abstract ComparisonToken : 'Cmp

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

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

    override x.GetHashCode() = x.ComparisonToken.GetHashCode()

// example

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


ComparableType(typeof<int>) < ComparableType(typeof<int>)
namespace System
Multiple items
type AbstractClassAttribute =
  inherit Attribute
  new : unit -> AbstractClassAttribute

Full name: Microsoft.FSharp.Core.AbstractClassAttribute

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

Full name: Script.ProjectionComparison<_,_>

--------------------
new : unit -> ProjectionComparison<'Id,'Cmp>
abstract 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
Multiple items
type ComparableType =
  inherit ProjectionComparison<ComparableType,string>
  new : t:Type -> ComparableType
  override ComparisonToken : string
  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
override ComparableType.ComparisonToken : string

Full name: Script.ComparableType.ComparisonToken
property Type.AssemblyQualifiedName: string
val __ : ComparableType
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<_>
Next Version Raw view Test code New version

More information

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