Skip to content
Merged
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
2 changes: 1 addition & 1 deletion Source/FunicularSwitch/FunicularSwitch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

<!--#region adapt versions here-->
<MajorVersion>6</MajorVersion>
<MinorAndPatchVersion>4.0</MinorAndPatchVersion>
<MinorAndPatchVersion>4.1</MinorAndPatchVersion>
<!--#endregion-->

<AssemblyVersion>$(MajorVersion).0.0</AssemblyVersion>
Expand Down
7 changes: 5 additions & 2 deletions Source/FunicularSwitch/Option.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,12 @@ public T GetValueOrThrow(string? errorMessage = null) =>
: 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);
Match(s => Option<TOther>.Some((TOther)(object)s!), Option<TOther>.None);

public override string ToString() => Match(v => v?.ToString() ?? "", () => $"None {typeof(T).BeautifulName()}");
public Option<TTarget> As<TTarget>() where TTarget : class =>
Bind(item => (item as TTarget).ToOption());

public override string ToString() => Match(v => v?.ToString() ?? "", () => $"None {typeof(T).BeautifulName()}");

public bool Equals(Option<T> other) =>
_isSome == other._isSome && EqualityComparer<T>.Default.Equals(_value, other._value);
Expand Down
14 changes: 14 additions & 0 deletions Source/Tests/FunicularSwitch.Test/OptionSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,20 @@ public void As_DoesNotMatch_None()
asList.Should().BeNone();
}

[TestMethod]
public void AsConvertOptionValue()
{
var value = new MyDerived();
var myBaseOption = Some(value);
myBaseOption.As<MyDerived>().Should().BeSome().Which.Should().Be(value);
myBaseOption.As<MyBase>().Should().BeSome().Which.Should().Be(value);
myBaseOption.As<AnotherClass>().Should().BeNone();
}

class MyBase;
class MyDerived : MyBase;
class AnotherClass;

[TestMethod]
public async Task Match_TaskOptionT_FuncTTout_FuncTOut_Some_Matched()
{
Expand Down
Loading