Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 45 additions & 9 deletions Source/FunicularSwitch/Option.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ public interface IOption
public Option<T1> Bind<T1>(Func<T, Option<T1>> map) => Match(map, Option<T1>.None);

public Task<Option<T1>> Bind<T1>(Func<T, Task<Option<T1>>> bind) => Match(bind, () => Option<T1>.None);

public Option<T> OrElse(Option<T> other) => OrElse(() => other);
public Option<T> OrElse(Func<Option<T>> other) => Match(Option.Some, other);
public Task<Option<T>> OrElse(Task<Option<T>> other) => OrElse(() => other);
public Task<Option<T>> OrElse(Func<Task<Option<T>>> other) => Match(Option.Some, other);

public void Match(Action<T> some, Action? none = null)
{
Match(some.ToFunc(), none?.ToFunc<int>() ?? (() => 42));
}
public void Match(Action<T> some, Action? none = null) => Match(some.ToFunc(), none?.ToFunc<int>() ?? (() => 42));

public async Task Match(Func<T, Task> some, Func<Task>? none = null)
{
Expand All @@ -79,6 +81,16 @@ public async Task<TResult> Match<TResult>(Func<T, Task<TResult>> some, Func<Task

return await none().ConfigureAwait(false);
}

@ax0l0tl ax0l0tl Jan 31, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this actually a breaking change? -> GetValueOrDefault...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean the match? In certain cases, as seen in GetValueOrDefault, it breaks the C# type inference.

public async Task<TResult> Match<TResult>(Func<T, TResult> some, Func<Task<TResult>> none)
{
if (_isSome)
{
return some(_value);
}

return await none().ConfigureAwait(false);
}

public async Task<TResult> Match<TResult>(Func<T, Task<TResult>> some, Func<TResult> none)
{
Expand All @@ -104,15 +116,15 @@ public async Task<TResult> Match<TResult>(Func<T, Task<TResult>> some, TResult n

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

public IEnumerator<T> GetEnumerator() => Match(v => new[] { v }, Enumerable.Empty<T>).GetEnumerator();
public IEnumerator<T> GetEnumerator() => Match(v => [v], Enumerable.Empty<T>).GetEnumerator();

public T? GetValueOrDefault() => Match(v => (T?)v, () => default);
public T? GetValueOrDefault() => Match(v => v, default(T?));

public T GetValueOrDefault(Func<T> defaultValue) => Match(v => v, defaultValue);

public T GetValueOrDefault(T defaultValue) => Match(v => v, () => defaultValue);

public T GetValueOrThrow(string? errorMessage = null) => Match(v => v, () => throw new InvalidOperationException(errorMessage ?? "Cannot access value of none option"));
public T GetValueOrThrow(string? errorMessage = null) => Match(v => v, T () => throw new InvalidOperationException(errorMessage ?? "Cannot access value of none option"));

public Option<TOther> Convert<TOther>() => Match(s => Option<TOther>.Some((TOther)(object)s!), Option<TOther>.None);

Expand Down Expand Up @@ -181,22 +193,46 @@ public static async Task<Option<TOut>> Bind<T, TOut>(this Task<Option<T>> bind,
var result = await bind.ConfigureAwait(false);
return await result.Bind(convert).ConfigureAwait(false);
}

public static async Task<Option<T>> OrElse<T>(this Task<Option<T>> option, Option<T> other)
Comment thread
park-jasper marked this conversation as resolved.
{
var result = await option.ConfigureAwait(false);
return result.OrElse(other);
}

public static async Task<Option<T>> OrElse<T>(this Task<Option<T>> option, Func<Option<T>> other)
{
var result = await option.ConfigureAwait(false);
return result.OrElse(other);
}

public static async Task<Option<T>> OrElse<T>(this Task<Option<T>> option, Task<Option<T>> other)
{
var result = await option.ConfigureAwait(false);
return await result.OrElse(other).ConfigureAwait(false);
}

public static async Task<Option<T>> OrElse<T>(this Task<Option<T>> option, Func<Task<Option<T>>> other)
{
var result = await option.ConfigureAwait(false);
return await result.OrElse(other).ConfigureAwait(false);
}

public static IEnumerable<TOut> Choose<T, TOut>(this IEnumerable<T> items, Func<T, Option<TOut>> choose) => items.SelectMany(i => choose(i));

public static Option<T> ToOption<T>(this Result<T> result) => ToOption(result, null);

public static Option<T> ToOption<T>(this Result<T> result, Action<string>? logError) =>
result.Match(
ok => Option.Some(ok),
Option.Some,
error =>
{
logError?.Invoke(error);
return Option<T>.None;
});

public static Result<T> ToResult<T>(this Option<T> option, Func<string> errorIfNone) =>
option.Match(s => Result.Ok(s), () => Result.Error<T>(errorIfNone()));
option.Match(Result.Ok, () => Result.Error<T>(errorIfNone()));

#region query-expression pattern

Expand Down
200 changes: 200 additions & 0 deletions Source/Tests/FunicularSwitch.Test/OptionSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@ public void NullCoalescingWithResultBoolBehavesAsExpected()
result.Should().BeError().Subject.Should().Be("Value is missing");
}

[TestMethod]
public async Task OrElse()
{
var none = None<int>();
none.OrElse(Option<int>.None).Should().BeNone();
ShouldBeSome42(none.OrElse(42));

var some = Some(42);
ShouldBeSome42(some.OrElse(Option<int>.None));
ShouldBeSome42(some.OrElse(2));

ShouldBeSome42(await none.OrElse(() => Task.FromResult(Some(42))));
return;

void ShouldBeSome42(Option<int> option) => option.Should().BeSome().Which.Should().Be(42);
}

[TestMethod]
public void QueryExpressionSelect()
{
Expand Down Expand Up @@ -217,6 +234,189 @@ public void NullOptionsWork()
nullOption.Equals(Option<MyClass?>.Some(null)).Should().BeTrue();
}

[TestMethod]
public void MatchWithActionExecutesSomeWhenSome()
{
var called = false;
Some(42).Match(x => called = x == 42);
called.Should().BeTrue();
}

[TestMethod]
public void MatchWithActionExecutesNoneWhenNone()
{
var called = false;
None<int>().Match(_ => { }, () => called = true);
called.Should().BeTrue();
}

[TestMethod]
public async Task MatchWithAsyncActionExecutesSomeWhenSome()
{
var called = false;
await Some(42).Match(
async x =>
{
await Task.Yield();
called = x == 42;
});
called.Should().BeTrue();
}

[TestMethod]
public async Task MatchWithAsyncActionExecutesNoneWhenNone()
{
var called = false;
await None<int>().Match(
async _ => await Task.Yield(),
async () =>
{
await Task.Yield();
called = true;
});
called.Should().BeTrue();
}

[TestMethod]
public void MatchWithFuncReturnsResultFromSomeWhenSome()
{
var result = Some(42).Match(x => x * 2, () => 0);
result.Should().Be(84);
}

[TestMethod]
public void MatchWithFuncReturnsResultFromNoneWhenNone()
{
var result = None<int>().Match(x => x * 2, () => 99);
result.Should().Be(99);
}

[TestMethod]
public void MatchWithValueReturnsValueWhenNone()
{
var result = None<int>().Match(x => x * 2, 99);
result.Should().Be(99);
}

[TestMethod]
public void MatchWithValueReturnsResultFromSomeWhenSome()
{
var result = Some(42).Match(x => x * 2, 0);
result.Should().Be(84);
}

[TestMethod]
public async Task MatchWithAsyncFuncBothReturnsResultFromSomeWhenSome()
{
var result = await Some(42).Match(
async x =>
{
await Task.Yield();
return x * 2;
},
async () =>
{
await Task.Yield();
return 0;
});
result.Should().Be(84);
}

[TestMethod]
public async Task MatchWithAsyncFuncBothReturnsResultFromNoneWhenNone()
{
var result = await None<int>().Match(
async x =>
{
await Task.Yield();
return x * 2;
},
async () =>
{
await Task.Yield();
return 99;
});
result.Should().Be(99);
}

[TestMethod]
public async Task MatchWithSyncSomeAsyncNoneReturnsResultFromSomeWhenSome()
{
var result = await Some(42).Match(
x => x * 2,
async () =>
{
await Task.Yield();
return 0;
});
result.Should().Be(84);
}

[TestMethod]
public async Task MatchWithSyncSomeAsyncNoneReturnsResultFromNoneWhenNone()
{
var result = await None<int>().Match(
x => x * 2,
async () =>
{
await Task.Yield();
return 99;
});
result.Should().Be(99);
}

[TestMethod]
public async Task MatchWithAsyncSomeSyncNoneReturnsResultFromSomeWhenSome()
{
var result = await Some(42).Match(
async x =>
{
await Task.Yield();
return x * 2;
},
() => 0);
result.Should().Be(84);
}

[TestMethod]
public async Task MatchWithAsyncSomeSyncNoneReturnsResultFromNoneWhenNone()
{
var result = await None<int>().Match(
async x =>
{
await Task.Yield();
return x * 2;
},
() => 99);
result.Should().Be(99);
}

[TestMethod]
public async Task MatchWithAsyncSomeValueNoneReturnsResultFromSomeWhenSome()
{
var result = await Some(42).Match(
async x =>
{
await Task.Yield();
return x * 2;
},
84);
result.Should().Be(84);
}

[TestMethod]
public async Task MatchWithAsyncSomeValueNoneReturnsResultFromNoneWhenNone()
{
var result = await None<int>().Match(
async x =>
{
await Task.Yield();
return x * 2;
},
99);
result.Should().Be(99);
}

class MyClass;

class MyOtherClass;
Expand Down