From e3ca97dbd4cdd069de5e3a05ddbb4e550113aa4b Mon Sep 17 00:00:00 2001 From: Alexander Wiedemann Date: Tue, 9 Dec 2025 18:21:33 +0100 Subject: [PATCH 1/3] - implement As on Option directly to avoid misguided use of As extension on Option --- Source/FunicularSwitch/Option.cs | 7 +++++-- Source/Tests/FunicularSwitch.Test/OptionSpecs.cs | 13 +++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Source/FunicularSwitch/Option.cs b/Source/FunicularSwitch/Option.cs index ea4813d..78cfdfc 100644 --- a/Source/FunicularSwitch/Option.cs +++ b/Source/FunicularSwitch/Option.cs @@ -128,9 +128,12 @@ public T GetValueOrThrow(string? errorMessage = null) => : throw new InvalidOperationException(errorMessage ?? "Cannot access value of none option"); public Option Convert() => - Match(s => Option.Some((TOther)(object)s!), Option.None); + Match(s => Option.Some((TOther)(object)s!), Option.None); - public override string ToString() => Match(v => v?.ToString() ?? "", () => $"None {typeof(T).BeautifulName()}"); + public Option As() 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 other) => _isSome == other._isSome && EqualityComparer.Default.Equals(_value, other._value); diff --git a/Source/Tests/FunicularSwitch.Test/OptionSpecs.cs b/Source/Tests/FunicularSwitch.Test/OptionSpecs.cs index ff69b50..6ce4dd2 100644 --- a/Source/Tests/FunicularSwitch.Test/OptionSpecs.cs +++ b/Source/Tests/FunicularSwitch.Test/OptionSpecs.cs @@ -595,6 +595,19 @@ public void As_DoesNotMatch_None() asList.Should().BeNone(); } + [TestMethod] + public void AsConvertOptionValue() + { + var myBaseOption = Some(new MyDerived()); + myBaseOption.As().IsSome().Should().BeTrue(); + myBaseOption.As().IsSome().Should().BeTrue(); + myBaseOption.As().IsSome().Should().BeFalse(); + } + + class MyBase; + class MyDerived : MyBase; + class AnotherClass; + [TestMethod] public async Task Match_TaskOptionT_FuncTTout_FuncTOut_Some_Matched() { From 23d2ff34b5e96bd832f5dcb33ae9959204ee2d9f Mon Sep 17 00:00:00 2001 From: Alexander Wiedemann Date: Tue, 9 Dec 2025 20:07:32 +0100 Subject: [PATCH 2/3] - FunicularSwitch version to 6.4.1 --- Source/FunicularSwitch/FunicularSwitch.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/FunicularSwitch/FunicularSwitch.csproj b/Source/FunicularSwitch/FunicularSwitch.csproj index 7af2840..7858bd3 100644 --- a/Source/FunicularSwitch/FunicularSwitch.csproj +++ b/Source/FunicularSwitch/FunicularSwitch.csproj @@ -14,7 +14,7 @@ 6 - 4.0 + 4.1 $(MajorVersion).0.0 From 7d3fa6d63243fbebd68d3a0d3abf827a1708c3fb Mon Sep 17 00:00:00 2001 From: Jasper Park Date: Wed, 10 Dec 2025 15:57:57 +0100 Subject: [PATCH 3/3] test: use specific awesome assertions for option instead of checking against .IsSome() --- Source/Tests/FunicularSwitch.Test/OptionSpecs.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Source/Tests/FunicularSwitch.Test/OptionSpecs.cs b/Source/Tests/FunicularSwitch.Test/OptionSpecs.cs index 6ce4dd2..6d27b87 100644 --- a/Source/Tests/FunicularSwitch.Test/OptionSpecs.cs +++ b/Source/Tests/FunicularSwitch.Test/OptionSpecs.cs @@ -598,10 +598,11 @@ public void As_DoesNotMatch_None() [TestMethod] public void AsConvertOptionValue() { - var myBaseOption = Some(new MyDerived()); - myBaseOption.As().IsSome().Should().BeTrue(); - myBaseOption.As().IsSome().Should().BeTrue(); - myBaseOption.As().IsSome().Should().BeFalse(); + var value = new MyDerived(); + var myBaseOption = Some(value); + myBaseOption.As().Should().BeSome().Which.Should().Be(value); + myBaseOption.As().Should().BeSome().Which.Should().Be(value); + myBaseOption.As().Should().BeNone(); } class MyBase;