Skip to content

Commit

Permalink
Replace Error.New("None") with Errors.None
Browse files Browse the repository at this point in the history
And added ToString to Error types
  • Loading branch information
louthy committed Jun 28, 2022
1 parent 5bf6e3b commit 39d8a2b
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 35 deletions.
14 changes: 13 additions & 1 deletion LanguageExt.Core/Common/Error.cs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,9 @@ public record Expected(string Message, int Code, Option<Error> Inner = default)
public override Option<Error> Inner { get; } =
Inner;

public override string ToString() =>
Message;

/// <summary>
/// Generates a new `ErrorException` that contains the `Code`, `Message`, and `Inner` of this `Error`.
/// </summary>
Expand Down Expand Up @@ -407,6 +410,9 @@ public record Exceptional(string Message, int Code) : Error
[DataMember]
public override int Code { get; } = Code;

public override string ToString() =>
Message;

/// <summary>
/// Returns the inner exception as an `Error` (if one exists), `None` otherwise
/// </summary>
Expand Down Expand Up @@ -477,6 +483,9 @@ public sealed record BottomError() : Exceptional(BottomException.Default)

public override string Message =>
Errors.BottomText;

public override string ToString() =>
Message;

/// <summary>
/// Gets the Exception
Expand Down Expand Up @@ -537,7 +546,10 @@ public sealed record ManyErrors([property: DataMember] Seq<Error> Errors) : Erro

public override string Message { get; } =
Errors.ToFullArrayString();


public override string ToString() =>
Errors.ToFullArrayString();

/// <summary>
/// Gets the `Exception`
/// </summary>
Expand Down
9 changes: 7 additions & 2 deletions LanguageExt.Core/Common/ErrorException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,14 @@ public sealed class ManyExceptions : ErrorException
[Serializable]
public class BottomException : ExceptionalException
{
public static readonly BottomException Default = new ();
public static readonly BottomException Default;

public BottomException() : base(Default)
static BottomException()
{
Default = new();
}

public BottomException() : base(Errors.BottomText, Errors.BottomCode)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ public Option<A> Do(Action<A> f)
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Eff<A> ToEff() =>
ToEff(Error.New("None"));
ToEff(Errors.None);

/// <summary>
/// Convert the structure to an Eff
Expand All @@ -597,7 +597,7 @@ public Option<A> Do(Action<A> f)
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Aff<A> ToAff() =>
ToAff(Error.New("None"));
ToAff(Errors.None);

/// <summary>
/// Convert the structure to an Aff
Expand All @@ -620,7 +620,7 @@ public Option<A> Do(Action<A> f)
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Fin<A> ToFin() =>
ToFin(Error.New("None"));
ToFin(Errors.None);

/// <summary>
/// Convert the structure to a Fin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ public async Task<string> ToStringAsync()
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Aff<A> ToAff() =>
ToAff(Error.New("None"));
ToAff(Errors.None);

/// <summary>
/// Convert the structure to an Aff
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ public OptionUnsafe<A> Do(Action<A> f)
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Eff<A> ToEff() =>
ToEff(Error.New("None"));
ToEff(Errors.None);

/// <summary>
/// Convert the structure to an Eff
Expand All @@ -557,7 +557,7 @@ public OptionUnsafe<A> Do(Action<A> f)
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Aff<A> ToAff() =>
ToAff(Error.New("None"));
ToAff(Errors.None);

/// <summary>
/// Convert the structure to an Aff
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ public static Unit Match<A>(this TryOption<A> self, Action<A> Some, Action None,
public static Eff<A> ToEff<A>(this TryOption<A> ma) =>
Prelude.EffMaybe(() =>
ma.Match(Some: Fin<A>.Succ,
None: () => Fin<A>.Fail(Error.New("None")),
None: () => Fin<A>.Fail(Errors.None),
Fail: e => Fin<A>.Fail(e)));

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1154,7 +1154,7 @@ public static async Task<Unit> Match<A>(this TryOptionAsync<A> self, Action<A> S
public static Aff<A> ToAff<A>(this TryOptionAsync<A> ma) =>
Prelude.AffMaybe(async () => await
ma.Match(Some: Fin<A>.Succ,
None: () => Fin<A>.Fail(Error.New("None")),
None: () => Fin<A>.Fail(Errors.None),
Fail: e => Fin<A>.Fail(e)));

/// <summary>
Expand Down
41 changes: 41 additions & 0 deletions Samples/TestBed/ApplicativeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace TestBed;

using System;
using System.Diagnostics;
using LanguageExt;
using System.Threading.Tasks;
using static LanguageExt.Prelude;

public static class ApplicativeTest
{
static Aff<Unit> delay(int milliseconds) =>
Aff(async () =>
{
await Task.Delay(milliseconds);
return unit;
});

static Aff<int> parse(string str) =>
from x in parseInt(str).ToAff()
from _ in delay(1000)
select x;

static Aff<int> add(string sx, string sy) =>
SuccessAff((int x, int y) => x + y)
.Apply(parse(sx))
.Apply(parse(sy));

public static async Task Test()
{
await Report(add("100", "200"));
await Report(add("zzz", "yyy"));
}

static async Task Report<A>(Aff<A> ma)
{
var sw = Stopwatch.StartNew();
var r = await ma.Run();
sw.Stop();
Console.WriteLine($"Result: {r} in {sw.ElapsedMilliseconds}ms");
}
}
25 changes: 1 addition & 24 deletions Samples/TestBed/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,30 +72,7 @@ static void Main(string[] args)
//testing.Run(Runtime.New());
//var _ = QueueExample<Runtime>.Issue1065().RunUnit(new Runtime()).Result;
//ScheduleTests.Run();

Aff<Unit> delay(int milliseconds) =>
Aff(async () =>
{
await Task.Delay(milliseconds);
return unit;
});

Aff<A> delayed<A>(A x) =>
from _ in delay(1000)
select x;
var effect = SuccessAff((string sx, string sy) =>
from x in parseInt(sx)
from y in parseInt(sy)
select x + y)
.Apply(delayed("100"))
.Apply(delayed("200"));

var sw = Stopwatch.StartNew();
var r = effect.Run().GetAwaiter().GetResult();
sw.Stop();

Console.Write($"Result: {r} in {sw.ElapsedMilliseconds}ms");
ApplicativeTest.Test().GetAwaiter().GetResult();
}

public static async Task PipesTest()
Expand Down

0 comments on commit 39d8a2b

Please sign in to comment.