Skip to content

Commit

Permalink
Record types in C#
Browse files Browse the repository at this point in the history
  • Loading branch information
louthy committed Jul 30, 2017
1 parent dbfbcf8 commit 567c851
Show file tree
Hide file tree
Showing 8 changed files with 794 additions and 100 deletions.
44 changes: 44 additions & 0 deletions LanguageExt.Core/DataTypes/Record/Record.cs
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);
}
}
42 changes: 42 additions & 0 deletions LanguageExt.Core/DataTypes/Record/RecordUtil.cs
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");
}
}
}
2 changes: 1 addition & 1 deletion LanguageExt.Core/LanguageExt.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<DefineConstants>COREFX</DefineConstants>
</PropertyGroup>
<PropertyGroup>
<PackageVersion>2.0.91</PackageVersion>
<PackageVersion>2.1.0</PackageVersion>
<PackageId>LanguageExt.Core</PackageId>
<Title>LanguageExt.Core</Title>
<Authors>Paul Louth</Authors>
Expand Down
95 changes: 0 additions & 95 deletions LanguageExt.Core/Utility/Cast.cs

This file was deleted.

Loading

0 comments on commit 567c851

Please sign in to comment.