-
-
Notifications
You must be signed in to change notification settings - Fork 420
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
794 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
|
||
namespace LanguageExt | ||
{ | ||
/// <summary> | ||
/// Base class for types that are 'records'. A record has a set of readonly *fields( | ||
/// that make up its data structure. By deriving from this you get structural equality, | ||
/// structural ordering (`IComparable`), structural hashing (`GetHashCode`) as well as the | ||
/// operators `==`, `!=`, `<`, `<=`, `>`, `>=`, | ||
/// </summary> | ||
/// <typeparam name="RECORDTYPE"></typeparam> | ||
public abstract class Record<RECORDTYPE> : IEquatable<RECORDTYPE>, IComparable<RECORDTYPE> | ||
{ | ||
public static bool operator==(Record<RECORDTYPE> x, Record<RECORDTYPE> y) => | ||
RecordType<RECORDTYPE>.Equality((RECORDTYPE)(object)x, (RECORDTYPE)(object)y); | ||
|
||
public static bool operator !=(Record<RECORDTYPE> x, Record<RECORDTYPE> y) => | ||
!(x == y); | ||
|
||
public static bool operator >(Record<RECORDTYPE> x, Record<RECORDTYPE> y) => | ||
RecordType<RECORDTYPE>.Compare((RECORDTYPE)(object)x, (RECORDTYPE)(object)y) > 0; | ||
|
||
public static bool operator >=(Record<RECORDTYPE> x, Record<RECORDTYPE> y) => | ||
RecordType<RECORDTYPE>.Compare((RECORDTYPE)(object)x, (RECORDTYPE)(object)y) >= 0; | ||
|
||
public static bool operator <(Record<RECORDTYPE> x, Record<RECORDTYPE> y) => | ||
RecordType<RECORDTYPE>.Compare((RECORDTYPE)(object)x, (RECORDTYPE)(object)y) < 0; | ||
|
||
public static bool operator <=(Record<RECORDTYPE> x, Record<RECORDTYPE> y) => | ||
RecordType<RECORDTYPE>.Compare((RECORDTYPE)(object)x, (RECORDTYPE)(object)y) <= 0; | ||
|
||
public override int GetHashCode() => | ||
RecordType<RECORDTYPE>.Hash((RECORDTYPE)(object)this); | ||
|
||
public override bool Equals(object obj) => | ||
RecordType<RECORDTYPE>.Equality((RECORDTYPE)(object)this, obj); | ||
|
||
public int CompareTo(RECORDTYPE other) => | ||
RecordType<RECORDTYPE>.Compare((RECORDTYPE)(object)this, other); | ||
|
||
public bool Equals(RECORDTYPE other) => | ||
RecordType<RECORDTYPE>.Equality((RECORDTYPE)(object)this, other); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System; | ||
|
||
namespace LanguageExt | ||
{ | ||
/// <summary> | ||
/// Supports the building of record types (classes with sets of readonly field values) | ||
/// Provides structural equality testing, ordering, and hashing | ||
/// </summary> | ||
/// <typeparam name="A">Type to provide methods for</typeparam> | ||
public static class RecordType<A> | ||
{ | ||
/// <summary> | ||
/// General hash code function for record types | ||
/// </summary> | ||
public static readonly Func<A, int> Hash = | ||
IL.GetHashCode<A>(); | ||
|
||
/// <summary> | ||
/// General equality function for record types | ||
/// </summary> | ||
public static readonly Func<A, object, bool> Equality = | ||
IL.Equals<A>(); | ||
|
||
/// <summary> | ||
/// General typed equality function for record types | ||
/// </summary> | ||
public static readonly Func<A, A, bool> EqualityTyped = | ||
IL.EqualsTyped<A>(); | ||
|
||
/// <summary> | ||
/// General typed comparison function for record types | ||
/// </summary> | ||
public static readonly Func<A, A, int> Compare = | ||
IL.Compare<A>(); | ||
|
||
[Obsolete("Don't use Equals - use either RecordType<A>.Equality or RecordType<A>.EqualityTyped")] | ||
public new static bool Equals(object objA, object objB) | ||
{ | ||
throw new InvalidOperationException("Don't use Equals - use either RecordType<A>.Equality or RecordType<A>.EqualityTyped"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.