Skip to content

Commit e6cadfb

Browse files
committed
vkhorikov#317 Add non-generic Maybe.None
1 parent 2211ce5 commit e6cadfb

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

CSharpFunctionalExtensions.Tests/MaybeTests/BasicTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,15 @@ public void Maybe_From_without_type_parameter_creates_new_maybe()
126126
withoutTypeParam.Should().NotBe(differentValueTypeParam);
127127
}
128128

129+
[Fact]
130+
public void Can_cast_non_generic_maybe_none_to_maybe_none()
131+
{
132+
Maybe<int> maybe = Maybe.None;
133+
134+
maybe.HasValue.Should().BeFalse();
135+
maybe.HasNoValue.Should().BeTrue();
136+
}
137+
129138
private class MyClass
130139
{
131140
public override string ToString()

CSharpFunctionalExtensions/Maybe/Maybe.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public static implicit operator Maybe<T>(T value)
4747
return new Maybe<T>(value);
4848
}
4949

50+
public static implicit operator Maybe<T>(Maybe value) => None;
51+
5052
public static Maybe<T> From(T obj)
5153
{
5254
return new Maybe<T>(obj);
@@ -127,10 +129,12 @@ public override string ToString()
127129
}
128130

129131
/// <summary>
130-
/// Non-generic entrypoint for <see cref="Maybe{T}" /> methods
132+
/// Non-generic entrypoint for <see cref="Maybe{T}" /> members
131133
/// </summary>
132-
public static class Maybe
134+
public struct Maybe
133135
{
136+
public static Maybe None => new Maybe();
137+
134138
/// <summary>
135139
/// Creates a new <see cref="Maybe{T}" /> from the provided <paramref name="value"/>
136140
/// </summary>

0 commit comments

Comments
 (0)