Skip to content

Commit 76bf5fd

Browse files
committed
allowing overrides for ShallowEquality
1 parent a11eaa7 commit 76bf5fd

3 files changed

Lines changed: 31 additions & 14 deletions

File tree

RELEASE_NOTES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
### 0.0.25
2+
* allowing overrides for ShallowEquality via `ShallowEquality<'T>.Set(...)`
3+
14
### 0.0.24
25
* weak callbacks are now correctly GC'ed individually
36
* some new IndexList combinators

src/FSharp.Data.Adaptive/ShallowEquality.fs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,23 @@ module private ShallowEqualityHelpers =
1313
let inline hash (a : 'a) = (a :> obj).GetHashCode()
1414

1515
type ShallowEqualityComparer<'a> private() =
16-
static let instance = ShallowEqualityComparer<'a>() :> System.Collections.Generic.IEqualityComparer<'a>
16+
static let mutable instance =
17+
{ new System.Collections.Generic.IEqualityComparer<'a> with
18+
member x.GetHashCode v = ShallowEqualityHelpers.hash v
19+
member x.Equals(a,b) = ShallowEqualityHelpers.equals a b
20+
}
1721

1822
static member Instance = instance
23+
24+
static member Set(newInstance : System.Collections.Generic.IEqualityComparer<'a>) =
25+
instance <- newInstance
1926

20-
static member ShallowHashCode v = ShallowEqualityHelpers.hash v
21-
static member ShallowEquals(a,b) = ShallowEqualityHelpers.equals a b
27+
static member ShallowHashCode v = instance.GetHashCode v
28+
static member ShallowEquals(a,b) = instance.Equals(a,b)
2229

2330
interface System.Collections.Generic.IEqualityComparer<'a> with
24-
member x.GetHashCode v = ShallowEqualityHelpers.hash v
25-
member x.Equals(a,b) = ShallowEqualityHelpers.equals a b
31+
member x.GetHashCode v = instance.GetHashCode v
32+
member x.Equals(a,b) = instance.Equals(a,b)
2633

2734
#else
2835
open System.Reflection.Emit
@@ -172,15 +179,22 @@ type ShallowEqualityComparer<'a> private() =
172179
else
173180
fun (a : 'a) (b : 'a) -> System.Object.ReferenceEquals(a :> obj, b :> obj)
174181

175-
static let instance = ShallowEqualityComparer<'a>() :> System.Collections.Generic.IEqualityComparer<'a>
182+
static let mutable instance =
183+
{ new System.Collections.Generic.IEqualityComparer<'a> with
184+
member x.GetHashCode v = getHashCode v
185+
member x.Equals(a,b) = equals a b
186+
}
176187

177188
static member Instance = instance
178189

179-
static member ShallowHashCode v = getHashCode v
180-
static member ShallowEquals(a,b) = equals a b
190+
static member Set(newInstance : System.Collections.Generic.IEqualityComparer<'a>) =
191+
instance <- newInstance
192+
193+
static member ShallowHashCode v = instance.GetHashCode v
194+
static member ShallowEquals(a,b) = instance.Equals(a,b)
181195

182196
interface System.Collections.Generic.IEqualityComparer<'a> with
183-
member x.GetHashCode v = getHashCode v
184-
member x.Equals(a,b) = equals a b
197+
member x.GetHashCode v = instance.GetHashCode v
198+
member x.Equals(a,b) = instance.Equals(a,b)
185199

186200
#endif

src/FSharp.Data.Adaptive/Utilities/Utilities.fs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,11 @@ module internal CheapEquality =
123123
open System.Runtime.CompilerServices
124124

125125
#if FABLE_COMPILER
126-
let cheapHash (a : 'T) = ShallowEqualityComparer<'T>.ShallowHashCode a
127-
let cheapEqual (a : 'T) (b : 'T) = ShallowEqualityComparer<'T>.ShallowEquals(a, b)
126+
let cheapHash (a : 'T) = ShallowEqualityComparer<'T>.Instance.GetHashCode a
127+
let cheapEqual (a : 'T) (b : 'T) = ShallowEqualityComparer<'T>.Instance.Equals(a, b)
128128
#else
129-
let cheapHash (a : 'T) = ShallowEqualityComparer<'T>.ShallowHashCode a
130-
let cheapEqual (a : 'T) (b : 'T) = ShallowEqualityComparer<'T>.ShallowEquals(a, b)
129+
let cheapHash (a : 'T) = ShallowEqualityComparer<'T>.Instance.GetHashCode a
130+
let cheapEqual (a : 'T) (b : 'T) = ShallowEqualityComparer<'T>.Instance.Equals(a, b)
131131
#endif
132132

133133
module internal Unchecked =

0 commit comments

Comments
 (0)